powershell - Referenceing Piped Element's Parent -


i'm trying write powershell go through folder , preform different operations depending on file name. issue i'm having when use switch case $_ contains base name (see below). there way access it's parent element? need full path of file. saw somewhere $_.parent.fullpath, seems not correct.

 get-childitem -path "c:\folder\" -filter "*.xls" | foreach-object {     switch -wildcard ($_.basename){        "*test1*" {             write-host "test1"             write-host $_.parent.fullname        }        "*test2*" {             write-host "test2"             write-host $_.parent.fullname        }        "*tets3*" {             write-host "test3"             write-host $_.parent.fullname        }     } } 

i know if changed swtich case if statements have access fullname.

when you're in switch statement, you're within new pipeline, lose reference parent object.

unless stored object elsewhere. instance, before switch statement, added $file = $_ store reference current object within $file. when run now, see:

$file = $_      switch -wildcard ($_.basename){        "*test1*" {             write-host "test1"             write-host $file.fullname} [...]  >test1 c:\temp\test1.txt test12 c:\temp\test12.txt test100 c:\temp\test100.txt 

you can reference current object again using $file , picking of properties. hope puts on right direction.


Popular posts from this blog