Search code examples
powershellget-childitem

Get-ChildItem can't find one specific directory?


I am writing a PowerShell script which is supposed to take a look at a directory, sort the children by name (numbered names) and check if there's a specific file inside. If there is, it's supposed to be copied somewhere, if not it's supposed to look at the next one. This is my code so far:

#[...]
$notExist = $true
#$LatestCiClient = [some directory from earlier in the script]
$buildDirectories = Get-ChildItem -Path $LatestCiClient | Sort Name -Descending

while ($notExist) {
    $currentDir = $buildDirectories | Select-Object -First 1
    $assembliesDir = Get-ChildItem -Path $currentDir.FullName -Include Desktop-Assemblies #Breakpoint
    $script:exe = Get-ChildItem -Path $assembliesDir -Include SomeFile.exe | Select -First 1
    if ($Script:exe.Exists) {
        $notExist = $false
    } else {
        if ($buildDirectories.Count -gt 0) {
            $buildDirectories = $buildDirectories | Select -Skip 1
        } else {
            $script:NoneFound = $true
            $script:notExist = $false
        }
    }
}
#[more Powershell that is supposed to copy $script:exe] 

I am getting the numbered directories ($buildDirectories), in the debugger I see the whole list.

I enter the while block (breakpoint is set at "#Breakpoint"), select the first directory to check ($currentDir`, which is again there and correct) and look for a folder called "Desktop-Assemblies".

I am looking at it, in the Explorer, right now, the folder is there, it's filled, it's not hidden (it is read-only but that shouldn't matter?), I am not doing anything I haven't done several times in the script - nothing. $assembliesDir is empty.

I tried using "Desktop-Assemblies", I tried Desktop*, I tried without the -Include . If I use $currentDir it says

Cannot find path [WeirdPath] because it doesn't exist

Weird Path being the FolderName of my $currentDir but the rest of the path is C:\Users\MyUserName.

If I use $currentDir.FullName it finds all the directories, INCLUDING the one I am looking for. I should probably add that the whole directory that is being searched is on another computer, it's a network drive.


Solution

  • Did you use this?

    Get-ChildItem -Path $currentDir.FullName | Where-Object {$_.Name -eq 'Desktop-Assemblies'}