locked
PoweShell String and Get-ChildItem Comparison RRS feed

  • Question

  • Hello all! I'm trying to compare a couple of string values for storage directories in a SQL Database to a Storage Location.

    I've got a string value $StorageInfo.Path  which outputs D:\Video.Storage

    when including Get-ChildItem -Directory $StorageInfo.Path -Name I received only the guid's next fecc7106b8324b298ebc351f853733f5. (These are directories in a folder pulled from a SQL database where the whole path for example would be D:\Video.Storage\fecc7106b8324b298ebc351f853733f5)

    What I'm looking for is a string to output $StorageInfo.Path and Get-ChildItem joined together to look like the following of each of the directories in my string taken from a database $StorageInfo. D:\Video.Storage\fecc7106b8324b298ebc351f853733f5

    so that I can compare and exclude my other string $Events.VideoPath which outputs D:\Video.Storage\DBCBB93266EA4EB2B38A326275E63359\ 

    What I have doesn't work right since I need to somehow join $StorageInfo.Path and Get-ChildItem -Directory $StorageInfo.Path -Name 

    $notinSQL = Get-ChildItem -Directory $StorageInfo.Path -Name -Exclude $Events.VideoPath

    OR

    $notinSQL = Get-ChildItem $StorageInfo.Path -Name | where ($_.Name -notlike $Events.VideoPath)


    Anyone have any ideas?


    Friday, November 18, 2016 12:29 AM

Answers

  • (Get-ChildItem -Directory $StorageInfo.Path | Select-Object FullName).FullName

    Friday, November 18, 2016 12:40 AM

All replies

  • (Get-ChildItem -Directory $StorageInfo.Path | Select-Object FullName).FullName

    Friday, November 18, 2016 12:40 AM
  • That's exactly what I needed! Thank you!!
    Friday, November 18, 2016 3:29 PM
  • But do you understand why it works?

    It's because Get-ChildItem, when getting child items from a file system, outputs file system objects. FullName is a property of file system objects.

    Select-Object selects object properties from the previous objects output from the pipeline.

    A shorter way to write this would be as follows:


    Get-ChildItem $StorageInfo.Path | Select-Object -ExpandProperty FullName
    

    On PowerShell 3.0 and later (I believe), you can shorten this to the following:


    (Get-ChildItem $StorageInfo.Path).FullName

    This is merely "syntactic sugar" for the previous, and is less clear if you're not as familiar with PowerShell.


    -- Bill Stewart [Bill_Stewart]

    Friday, November 18, 2016 3:44 PM