Bianca Borges
Bianca Borges

Reputation: 185

Powershell Test-Path returns false, but the file exists

My script should look for a file in a computer list (txt) and return true if the file exists and false if it doesn't.

The problem is, it's returning "False" even though the file DOES exist.

    $computers = Get-Content -Path C:\temp\SvcHosts\PSKILL_SvcHosts_Workstations.txt

foreach ($comp in $computers)
{
    Test-Path \\$comp\c$\Windows\myfolder\myapp.exe
}

When I run Test-Path \\COMPUTERNAME\c$\Windows\myfolder\myapp.exe alone it returns true as it should. When I run from the txt list, returns false when it shouldn't.

Any ideas?

Upvotes: 4

Views: 8027

Answers (2)

Bianca Borges
Bianca Borges

Reputation: 185

This sounds strange, but I just ran my script (without change it) in another machine and it worked as expected. The main difference between the two machines I tested the script is the version of PowerShell ISE.

Machine 1 ISE Version: 3.0 Test-Path returns false even if the file exists

Machine 2: ISE Version:2.0 Test-Path works fine.

Not sure if these versions have difference betweeen them about access or something. I have the impression that it returned false in Machine 1 because the script couldn't reach the computer (I was running it under admin privileges anyway).

I know it doesn't make much sense...but anyway, case closed. Many thanks to everyone that helped.

Upvotes: 2

Eris
Eris

Reputation: 7638

Use a formatting string to fill in the values:

$Computers | ForEach-Object { 
    Test-Path ('\\{0}\c$\Windows\myfolder\myapp.exe' -f $_) 
}

Upvotes: 0

Related Questions