PowerShell: Check Windows OS Version

Here is a PowerShell script to get the OS details of Windows Machines. The script makes use of CIM cmdlets. The script also provides uptime information.

function Get-cOSInfo
{
<#
.SYNOPSIS
PowerShell script to collect OS information

.DESCRIPTION
Get the OS, edition, and build version of Windows Servers

.PARAMETER ComputerName
Accepts a list of computer names or IP addresses

.EXAMPLE
Get-cOSInfo -ComputerName Server1,server2

.LINK
#>
[CmdletBinding()]
Param
(
# A computer name or list of computer names
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$true,Position=0)]
[string[]]$ComputerName = 'localhost',
[Parameter(Mandatory=$False,ValueFromPipelineByPropertyName=$false,Position=1)]
[ValidateNotNull()]
[System.Management.Automation.PSCredential]
[System.Management.Automation.Credential()]
$Credential = [System.Management.Automation.PSCredential]::Empty
)

$outObject = @()
foreach ($computer in $ComputerName)
{
$tempObject = Get-CimInstance Win32_OperatingSystem -ComputerName $computer -ErrorAction STOP |
Select-Object @{Name='ComputerName';Expression={$_.CSName}},
@{Name='Operating System';Expression={$_.Caption}},
@{Name='Architecture';Expression={$_.OSArchitecture}},
Version,Organization,
InstallDate,
@{Name='Uptime';Expression={"{0:N0} days {1:N0} hours" -f `
($_.LocalDateTime - $_.LastBootUpTime).Days, `
($_.LocalDateTime - $_.LastBootUpTime).Hours}}
$outObject += $tempObject
}
return $outObject
}

Get-cOSInfo

Comments

Popular posts from this blog

Check SQL Server Database Status

PowerShell and Azure Resource Graph

Static Code Analysis: Some Tools