One of the great features of PowerShell is how much you can get from a relatively simple one line command. For example. you might want to test if a computer is running a 64-bit operating system. You can find out with a command as simple as this.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
PS C:\> (get-wmiobject win32_operatingsystem -comp chi-dc01).OsArchitecture -match "64" True PS C:\> (get-wmiobject win32_operatingsystem -comp novo8).OsArchitecture -match "64" False
If you are running PowerShell 3 you could substitute Get-CimInstance. One thing to be aware of with this particular class is that the OSArchitecture property isn't valid on older operating systems like Windows Server 2003. You'll get an exception.
In this case I modified the WMI query to only return the OSArchitecture property, which doesn't exist. Otherwise I would just get false which might not be entirely true. Of course, I always get carried away so before I knew it I had turned this one line command into a function.
#requires -version 2.0 Function Test-Is64Bit { [cmdletbinding()] Param( [ValidateNotNullorEmpty()] [string]$Computername=$env:computername, [Alias("RunAs")] [System.Management.Automation.Credential()]$Credential = [System.Management.Automation.PSCredential]::Empty ) Try { #hash table of parameters to splat $wmiParams = @{ Class = 'win32_operatingsystem' Property = 'osarchitecture' ComputerName = $computername ErrorAction = 'Stop' } if ($credential) { Write-Verbose "Adding credential" $wmiParams.Add("Credential",$Credential) } Write-Verbose "Testing $computername" $data = Get-WmiObject @wmiParams Write-Verbose $data.OSArchitecture $data.OsArchitecture -match "64" } #try Catch { Switch -wildcard ($error[0].Exception.Message) { "The RPC Server*" { Write-Warning "Can't connect to server. Verify name and availability." } "Access is denied*" { Write-Warning "Access denied. Check your permissions." } "Invalid query*" { Write-Warning "WMI information not available. OS version may Windows 2003 or earlier." } Default { Write-Error $error[0] } } #switch } #catch Finally { Write-Verbose "Finished testing" } #finally } #close function
The function takes a computername and optionally a PSCredential. You can use a saved PSCredential object or specify the name and you will be prompted.
The other design element is that if there is an exception caught I have a Switch statement to checks the message and writes a custom warning.
There's no reason you can't use the one-liner. But if you want to add a bit more robustness and create a re-usable tool, it doesn't take much to turn it into a simple function.
Even easier.
[System.IntPtr]::Size
If the result is 4 then x86, 8 then x64
Easier is a relative term. Certainly it is less typing. But it assumes some knowledge of .NET and it only works locally. Doesn’t help me test a remote system.
we use the win32_computersystem class to identify the OS Architecture like the function below
Function Get-OSArchitecture {
param (
[string]$ComputerName
)
$OSarc = (Get-WMIObject win32_computersystem -ComputerName $ComputerName).SystemType
If ( $OSarc -match ’86’) { return “32-Bit”}
ElseIF ( $OSarc -match ’64’ ){ return “64-Bit” }
Else { return $Null }
} # end Function Get-OSArchitecture
Here’s a variation on your technique in a one line command:
PS Scripts:\> ([regex]”[x|X]\d{2}”).Match( (get-wmiobject win32_computersystem -comp serenity).SystemType).value
x64
PS Scripts:\> ([regex]”[x|X]\d{2}”).Match( (get-wmiobject win32_computersystem -comp novo8).SystemType).value
X86