Test-path on list of computers

15,535

Solution 1

Here's how I would do it. Pipe the file content to Select-Object and create two properties for each name, ComputerName and FileExist, where the later value is the result of Test-Path.

Get-Content c:\Users\jason\Documents\Scripts\Serverlist.txt | `
   Select-Object @{Name='ComputerName';Expression={$_}},@{Name='FolderExist';Expression={ Test-Path "\\$_\c$\program files\folder"}}

You should expect output similar to:

ComputerName FolderExist
------------ -----------
Computer1    False
Computer2    True

Solution 2

$a = Get-Content 'c:\Users\jason\Documents\Scripts\Serverlist.txt' 

$b = $a | %  { $_ + " - " + @(Test-Path $_)}

$b now contain list of server and true or false based on test-path value

if you need all in $a variable:

$a = Get-Content 'c:\Users\jason\Documents\Scripts\Serverlist.txt' |  %  { $_ + " - " + @(Test-Path $_)}
Share:
15,535
Jake
Author by

Jake

Updated on June 28, 2022

Comments

  • Jake
    Jake almost 2 years

    I need to check if a folder exists on multiple computers using powershell.

    I've started my script but have gotten myself muddled. How do I get the computer name from my serverlist.txt into the test path cmdlet?

    $a = Get-Content 'c:\Users\jason\Documents\Scripts\Serverlist.txt' foreach  ($i in $a)  {Test-Path "\\$a\c$\program files\folder"}