Check Powerhell version on remote computers

Ever needed to check for what Powershell version your clients have and have to do it on a lot of machines?

This script aims to help with that, it connects to the computers you specify in a text file and checks what Powershell version they have and if it is offline it will state so either in the console or the output file of your choice.

Depending on your preferred output needs the script gives you two options, one is directly to the console window and the other one is to a .CSV file that can be used in softwares like Excel to process it further.

Full output of the script is shown below, you can either copy the text or scroll to the end and get is as a .ZIP file.

CheckPSVersion.ps1

# Almost a simple script to check if a batch of computers are online and checks for which Powershell version they have.
#
# Usage:
# Output to console: .\CheckPSVersion.ps1 -File .\Computers.txt
# Output to file: .\CheckPSVersion.ps1 -File .\Computers.txt -Output .\version.csv
#
# Version: 1.0

# Required parameters.
param (
# -File is the file with all the computers to be checked, each on a seperate row in the document.
[Parameter(Mandatory=$true)][string]$File,
# -Output is if you want the result to end up in a file instead of the console, add desired file name.
[Parameter(Mandatory=$false)][string]$Output
)

# Checks to see if output file already exists.
if($Output.Length -gt "0"){
$FileExist = Get-ChildItem $Output -ErrorAction SilentlyContinue
If ($FileExist.Exists){
Write-Host "File already exist, please remove it or use another file nameā€¦"
exit 1
}
}

# Set counter for iteration
$RowCount = 0

# Set headers for CSV file
if($Output.Length -gt "0") {
Write-Output "Computer,PSVersion" | Out-File -FilePath $Output -Append
}

# Iterate through the list and present the result to the console or into a file.
foreach($Line in Get-Content $File) {
if($Line -match $regex){
$Status = Test-Connection $Line -Quiet
if($Status -eq 'True'){
if($Output.Length -gt "0"){
$psVer = Invoke-Command -ComputerName $Line -ScriptBlock {$PSVersionTable.PSVersion}
Write-Output "$($Line),$($psVer.Major).$($psVer.Minor)" | Out-File -FilePath $Output -Append
$RowCount++
Write-Output "$($RowCount) rows added to file"
}
else{
$psVer = Invoke-Command -ComputerName $Line -ScriptBlock {$PSVersionTable.PSVersion}
Write-Output "$($Line) has PSVersion: $($psVer.Major).$($psVer.Minor)"
}
}
else {
if($Output.Length -gt "0"){
Write-Output "$($Line),Offline" | Out-File -FilePath $Output -Append
$RowCount++
Write-Output "$($RowCount) rows added to file"
}
else{
Write-Output "$($Line) is Offline"
}
}
}
}

Download

Disclaimer

As always these scripts are releasedĀ AS-IS, usage of these script are at your on risk, i cannot guarantee functionality outside my environment.

Leave a Reply

Your email address will not be published. Required fields are marked *