Powershell – Test-path checking for access with powershell

powershell

I am trying to find out who on my network has Access installed. I have had several problems getting the list to work correctly, so I have used the output txt file. Anyway even if it's not a great way to do this it should still work. Can anyone tell me why this is not working?

$Computers = Get-QADComputer | select name | Out-File "c:\access_search.txt"
$Computers = Get-Content "c:\access_search.txt"
$Path = "\c$\Program Files\Microsoft Office\Office12\Access.pip"
$AccessPath = "\\" + $PCName + $Path
Foreach ($PCname in $Computers){
$Result = Test-Path $AccessPath
if ($Result -eq "True")
{$Pcname}
}

Best Answer

You're not testing the correct path since it's being assigned prior to the foreach loop where you define $PCname. This means that you're testing the path \\\c$\Program Files\Microsoft Office\Office12\Access.pip every time.

Try this:

$Computers = Get-QADComputer | select name | Out-File "c:\access_search.txt"
$Computers = Get-Content "c:\access_search.txt"
$Path = "\c$\Program Files\Microsoft Office\Office12\Access.pip"
Foreach ($PCname in $Computers){
    $AccessPath = "\\" + $PCName + $Path
    $Result = Test-Path $AccessPath
    if ($Result -eq "True")
        {$Pcname}
}