Script to see if directory exist on all user profiles from a list of computers

I have a list of computers (.txt file) that I need to see if a directory exist under each user profile, then output to csv with computer name, profile name and true/false.

Text file location: C:\temp\scripts\bluezoneexist.txt

Looking for this directory C:\users%profile%\documents\bluezone

Output contains: Machine name, Profile name, True/False

Output location: C:\temp\scripts\bluezoneexist.csv

 

 

So I have written previous scripts to look at a list of computers to see if a directory exist in Program Files and output the results:

#This file contains the list of machines you want to use

$computers = gc "C:\scripts\O365PathExist.txt"

#output file name and location

$outfile = "C:\scripts\O365PathExist.csv"

#removes existing output file if it exist

If(Test-Path $outfile){Remove-Item $outfile -Force}

#this pulls from the list of computers, looks to see if Office path exist and outputs results. If machine is off it will show as False

foreach ($computers in $computers) {

$content = Test-Path -path "\\$computers\c$\Program Files (x86)\Microsoft Office\root"

$content2 = Test-Path -path "\\$computers\c$\Program Files\Microsoft Office\root"

"$computers, $content, $content2" | Add-Content $outfile

}


And I have written scripts that looks at all user profiles on a single computer to see if a directory exist and output the results (profile name, true/false):

$outfile = "C:\temp\scripts\bluezone2.csv"

$paths = Get-ChildItem -Directory c:\users | Select-Object $_.Name

ForEach ($path in $paths){

  $content =  test-path -path "c:\users\$path\Documents\BlueZone"

  "$path, $content" | Add-Content $outfile

  } 

 

I tried this and it goes through the list of machines and outputs correctly, but the user profiles are from the machine in which I run it on and not the profiles from the list of machines:

#This file contains the list of machines you want to use

$computers = gc "C:\temp\scripts\BlueZoneExist.txt"

 

#output file name and location

$outfile = "C:\temp\scripts\BlueZoneExist.csv"

 

#removes existing output file if it exist

If(Test-Path $outfile){Remove-Item $outfile -Force}

 

#search user profiles

$paths = Get-ChildItem -Directory c:\users | Select-Object $_.Name

 

foreach ($computers in $computers)

{ForEach ($path in $paths){

  $content =  test-path -path "c:\users\$path\Documents\BlueZone"

  "$computers, $path, $content" | Add-Content $outfile

  }

 } 

  

But I cannot figure out how I can pull from a list of computers and loop through all user profiles on those computers to see if a directory exist and then output the results to a csv…

 

If I got you right you could start with something like this:

$computerList = Get-Content -Path 'C:\scripts\O365PathExist.txt'
$outfile = 'C:\scripts\O365PathExist.csv'

$ResultObject = New-Object System.Collections.Generic.List[object]
$ResultObject = Foreach ($ComputerName in $computerList) {
    $newobj = @{
        ComputerName = $ComputerName
    }
    if (Test-Connection -ComputerName $ComputerName -Count 1 -Quiet ) {
        $newobj['Online'] = $true
        $newobj['ProgramFilesX86'] = Test-Path -Path 'c:\Program Files (x86)\Microsoft Office\root'
        $newobj['ProgramFiles'] = Test-Path -Path 'c:\Program Files\Microsoft Office\root'
    }
    else {
        $newobj['Online'] = $false
        $newobj['ProgramFilesX86'] = 'n/a'
        $newobj['ProgramFiles'] = 'n/a'
    }
    [PSCustomObject]$newobj
}

$ResultObject | Select-Object -Property Computername, Online, Prog*
$ResultObject | 
Export-Csv -Path $outfile -NoTypeInformation

… and with something like this for your “BlueZone thingy” :wink:

$computerList = Get-Content -Path 'C:\Sample\BlueZoneExist.txt'
$outfile = 'C:\temp\scripts\BlueZoneExist.csv'

$ResultList = Foreach ($ComputerName in $computerList) {
    if (Test-Connection -ComputerName $ComputerName -Count 1 -Quiet ) {
       Invoke-Command -ComputerName $ComputerName -ScriptBlock {
            Get-ChildItem -Directory -Path C:\Users -Exclude public |
                ForEach-Object {
                    [PSCustomObject]@{
                        UserName = $_.Name
                        BlueZone = Test-Path (Join-Path -Path $_.FullName -ChildPath 'Documents\BlueZone')
                    }
                }
       } |
        Select-Object -ExcludeProperty RunspaceId
    }
}
$ResultList | Select-Object -Property PSComputerName,UserName,BlueZone
$ResultList | Export-Csv -Path $outfile -NoTypeInformation