locked
How to Get-Childitem from unknown drive letter? RRS feed

  • Question

  • btw, I am new to powershell. I will be using get-childitem as an example. In order to list items in drive F:\ i would use "Get-Childitem f:\". What if I have a script that will do this for me and the drive letter changes to G:. Would I then need to change the script every time the drive letter changes? Or what about if I have a folder called "Backups" in an external drive, how would I use the Get-Childitem cmdlet to view items in the "Backup" folder without providing or knowing the drive letter where the "Backup" folder is located?

    Thanks in advance and please let me know if you need any further details or explanation.

    Best Regards,

    Alex


    Up The Irons!
    Friday, April 29, 2011 2:13 PM

Answers

  • I assume this is a removable drive? well, if the drive has a label, you can do something like this to find it..

    $drive = (get-wmiobject win32_volume -filter 'label = "labelname"').name

    $drive will then be the g:\ part, so you can just add to it

    $drive += "backup\"

    all in one line...

    $path = (get-wmiobject win32_volume -filter 'label = "labelname"').name + "backup\"

    • Proposed as answer by A.Hultgren Friday, April 29, 2011 5:16 PM
    • Marked as answer by Alan Zhu Monday, May 2, 2011 8:43 AM
    Friday, April 29, 2011 2:25 PM
  • Thanks, Larry.

    Error handling should always be added :)

    $sa = New-Object -ComObject Shell.Application
    $Drive = @{
    FilterScript = {$_.Name -match "OneTouch4 Plus"}
    }
    $FindDrive = $sa.NameSpace(17).items() | Where-Object @Drive
    if ($FindDrive) {Get-ChildItem -Path $FindDrive.Path}
    Else {Write-Output "Cannot find specified drive"}

    Karl


    http://unlockpowershell.wordpress.com
    Co-Author, Windows PowerShell 2.0 Bible
    -join("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})
    • Marked as answer by Alan Zhu Monday, May 2, 2011 8:43 AM
    Friday, April 29, 2011 3:03 PM
  • Just for fun, here's my brief one-liner version
     
    ((New-Object -C Shell.Application).NameSpace(17).items()|?{$_.Name
    -match 'OneTouch4 Plus'})|%{ls $_.Path}
     
    I used it as a test for my ConvertScriptTo-FullNames function and got
    the expansion
     
    ((New-Object -ComObject
    Shell.Application).NameSpace(17).items()|Where-Object{$_.Name -match
    'OneTouch4 Plus'})|ForEach-Object{Get-ChildItem $_.Path}
     
    I have to trust that "garbage collection" cleans up the objects allocated.
     
    • Marked as answer by Alan Zhu Monday, May 2, 2011 8:43 AM
    Friday, April 29, 2011 5:46 PM

All replies

  • I assume this is a removable drive? well, if the drive has a label, you can do something like this to find it..

    $drive = (get-wmiobject win32_volume -filter 'label = "labelname"').name

    $drive will then be the g:\ part, so you can just add to it

    $drive += "backup\"

    all in one line...

    $path = (get-wmiobject win32_volume -filter 'label = "labelname"').name + "backup\"

    • Proposed as answer by A.Hultgren Friday, April 29, 2011 5:16 PM
    • Marked as answer by Alan Zhu Monday, May 2, 2011 8:43 AM
    Friday, April 29, 2011 2:25 PM
  • If you know the name of the drive, you can find it using the com object Shell.Application

    The following example will show files and folders on my external drive:

    $sa = New-Object -ComObject Shell.Application
    $Drive = @{
    FilterScript = {$_.Name -match "OneTouch4 Plus"}
    }
    $FindDrive = $sa.NameSpace(17).items() | Where-Object @Drive
    Get-ChildItem -Path $FindDrive.Path

    It's a version 2 script.

    Karl


    http://unlockpowershell.wordpress.com
    Co-Author, Windows PowerShell 2.0 Bible
    -join("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})
    Friday, April 29, 2011 2:33 PM
  • Here is an observation regarding that exact code.
    If no drive exists with a matching label, then the last command will
    show the current drive's contents.
     
    Friday, April 29, 2011 2:47 PM
  • Thanks, Larry.

    Error handling should always be added :)

    $sa = New-Object -ComObject Shell.Application
    $Drive = @{
    FilterScript = {$_.Name -match "OneTouch4 Plus"}
    }
    $FindDrive = $sa.NameSpace(17).items() | Where-Object @Drive
    if ($FindDrive) {Get-ChildItem -Path $FindDrive.Path}
    Else {Write-Output "Cannot find specified drive"}

    Karl


    http://unlockpowershell.wordpress.com
    Co-Author, Windows PowerShell 2.0 Bible
    -join("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})
    • Marked as answer by Alan Zhu Monday, May 2, 2011 8:43 AM
    Friday, April 29, 2011 3:03 PM
  • I've learned a lot from Karl, but not brevity :)

    $sa = New-Object -ComObject Shell.Application
    ($sa.NameSpace(17).items() | ? {$_.Name -match 'OneTouch4 Plus' }) | %{ gci $_.Path }

    With this, if OneTouch4 Plus isn't found, gci doesn't run.

    Friday, April 29, 2011 5:18 PM
  • Thanks :)

    As you know from other threads, I don't do brevity ;)

    Karl


    http://unlockpowershell.wordpress.com
    Co-Author, Windows PowerShell 2.0 Bible
    -join("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})
    Friday, April 29, 2011 5:43 PM
  • Just for fun, here's my brief one-liner version
     
    ((New-Object -C Shell.Application).NameSpace(17).items()|?{$_.Name
    -match 'OneTouch4 Plus'})|%{ls $_.Path}
     
    I used it as a test for my ConvertScriptTo-FullNames function and got
    the expansion
     
    ((New-Object -ComObject
    Shell.Application).NameSpace(17).items()|Where-Object{$_.Name -match
    'OneTouch4 Plus'})|ForEach-Object{Get-ChildItem $_.Path}
     
    I have to trust that "garbage collection" cleans up the objects allocated.
     
    • Marked as answer by Alan Zhu Monday, May 2, 2011 8:43 AM
    Friday, April 29, 2011 5:46 PM
  • BTW, I just uploaded ConvertScriptTo-FullNames-function.ps1
    to
     
    My code is mostly based on Joel 'Jaykul' Bennett's code at
    Joel did the heavy lifting and I just gave it my preferential wrapping
    and code style indentation (which I'm still experimenting with).
     
    You first execute
    . ConvertScriptTo-FullNames-function.ps1
    to instantiate the function and then you can execute
    ConvertScriptTo-FullNames anyscripttobeexpanded.ps1
    and redirect the output as desired.
     
    There are two switches -CommandNamesOnly and -ParameterNamesOnly to
    allow control over the expansion.
     
     
    Saturday, April 30, 2011 12:50 AM
  • Thanks!

     

    It's a lot of information for me now but I know with time I will be able to get it. I did try the script and it worked like I wanted. It's a learning expirience for me.

     

    Thanks!


    Up The Irons!
    Sunday, May 1, 2011 12:18 AM