Tuesday, January 16, 2018

Powershell script to find all folders in program files

$erroractionPreference="stop"
$pcs = Get-ADComputer -Filter "Name -like '*'" | select-object name
$pccount = $pcs.Count
$counter = 1
foreach ($pc in $pcs) {
    $computername = $pc.name
    if(Test-Connection -ComputerName $computername -Count 1 -Quiet){
        Try
        {
            $path = "\\" + $computername + "\c$\program files\"
            $programs1 = Get-ChildItem -Path $path
            foreach($program in $programs1){
                Write-Host $computername","$program
            }
            $path = "\\" + $computername + "\c$\program files (x86)\"
            $programs2 = Get-ChildItem -Path $path
            foreach($program in $programs2){
                Write-Host $computername","$program
            }

        }
        Catch
        {
        Write-Host $counter" of "$pccount" Search Error "$computername
        }
    }
    else{
        Write-Host $counter" of "$pccount" Search Connection Failure "$computername
    }
    $counter++

}

No comments:

Post a Comment