Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell Test-Path returns false, but the file exists

Tags:

powershell

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?

like image 850
Bianca Borges Avatar asked Nov 21 '25 04:11

Bianca Borges


1 Answers

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.

like image 85
Bianca Borges Avatar answered Nov 24 '25 00:11

Bianca Borges