Powershell fuzzy behavior with Get-ChildItem and wildcard in path

Windows 8.1 Enterprise

    $PSVersionTable.PSVersion
    Major  Minor  Build  Revision
    -----  -----  -----  --------
    4      0      -1     -1

      

This is what I see. I have a file "commands2.txt" in the "level2" subdirectory. For simplicity, this is the only file in the "test" structure. Full path c: \ users \ chris \ testing \ level2 \ commands2.txt

Setting from c: \ users \ chris \ testing as current directory.

    $stuff  = gci c:\users\chris\testing\ -Recurse command*.txt
    $stuff2 = gci c:\users\chris\testing\* -Recurse command*.txt

      

So now

    get-item $stuff 

      

returns "get-item: Cannot find path" C: \ Users \ chris \ testing \ commands2.txt because it does not exist. "Note that" level2 "is missing. It looks like just adding it. Name to the current one. paths.

But

    get-item $stuff2 

      

Returns the expected results of the get-item file fullpath.

Odd right? So here's where I got confused.

    Compare-Object $stuff $stuff2

      

Shows that they are different.

    InputObject                                                 SideIndicator
    -----------                                                 -------------
    C:\users\chris\testing\level2\commands2.txt                 =>
    commands2.txt                                               <=

      

But:

    $a = $stuff | select *
    $b = $stuff2 | select *
    compare-object $a $b -includeEqual

      

Shows that they are the same. I know I can work around this by putting "*" in the path.

But why are the variables different, and how can I tell?

Why is InputObject different?

Is there a way to see the difference between the two variables $ stuff and $ stuff2?

Update: Vesper basically nailed it. Searching here with get-childitem and tostring returned helpful questions. Mine seems to be a variation of several others. A cryptic other conversion to string [] of seemingly identical input shows what's going on. Also, there is a 2010 bug report here: https://connect.microsoft.com/PowerShell/feedback/details/556004/get-childitem-gets-fileinfo-constructed-in-different-ways-depending-on- parameters based on this previous link. Good times.

+3


source to share


1 answer


The difference is really subtle, you can tell by the result toString()

.

 PS > $stuff.toString()
 commands2.txt    
 PS > $stuff2.toString()
 C:\users\chris\testing\level2\commands2.txt 

      



Here's some data on MSDN here which indicates that it toString()

doesn't always return the full filename. You seem to be facing this problem. I refer to this as a bug, but there is a way to find if the error was running proc by querying toString()

and checking for fullName

.

 if ($stuff.toString() -ne $stuff.fullName) { Write-Host "Bug!" }

      

0


source







All Articles