Reputation: 31
What am I doing wrong here?
I want to find if a folder exists on a group of PC's (including my own) and if it does, I want it to tell me the name of the computer. If it isn't there, I want it to tell me the name of the computer and 'Path is not there'
$Computers = gc 'C:\Temp\Computers.txt'
foreach ($Computer in $Computers) {
if (Test-Path -Path '\\$Computer\C$\Temp2') {
Write-Host $Computer
} else {
Write-Host $Computer + 'Path Is Not There'
}
}
The above tells me that the folder is not there (I know it is for at least one of the computers in the list).
Upvotes: 0
Views: 199
Reputation: 3928
Try to change the single quotes ( ' ) for the Test-Path -Path into double quotes ( " ), otherwise the variables won't be recognized
Upvotes: 0
Reputation: 13237
Your issue is the quotes you are using around the path you are testing.
Single quotes do not allow variable expansion, so it's literally looking for the path '\\$Computer\C$\Temp2'
and not using the variable for the computer name.
Documentation on this: about_quoting_rules
If you update your code to use double quotes, the variable value will be used as you want:
Test-Path -Path "\\$Computer\C$\Temp2"
Upvotes: 6