Determine the Os Version, Linux and Windows from Powershell

Determine the OS version, Linux and Windows from Powershell

Aren't there environment variables you can view on the other platforms for the OS?

Get-ChildItem -Path Env:

Particularly, on Windows at least, there's an OS environment variable, so you should be able to accomplish this by using $Env:OS.


Since some time has passed and the PowerShell Core (v6) product is GA now (the Core branding has been dropped as of v7), you can more accurately determine your platform based on the following automatic boolean variables:

$IsMacOS
$IsLinux
$IsWindows

How to detect Linux or macOS or Windows in powershell?

The problem is Powershell 5.1.

Detailed Here: https://devblogs.microsoft.com/scripting/powertip-determine-your-version-of-powershell-and-host-operating-system/

More Detail Here: Determine the OS version, Linux and Windows from Powershell

Powershell 5.1 does not have the capacity nor ability to determine OS outside of the Microsoft environment. The best you can do is, using get-wmi or cim-sesssion

 windows 
!windows

For PowerShell Core (Powershell Version 6.0+), you can use Automatic Variables:

$IsLinux
$IsMacOS
$IsWindows

With 6+ you can do something to the effect of:

   foreach ($i in $info) {

if ($i -eq $IsLinux) {
Write-Host $i is Linux
}
elseif ($i -eq $IsMacOS) {
Write-Host $i is This is a dirty, dirty Mac
}
elseif ($i -eq $IsWindows) {
Write-Host $i is Windows
}

}

To bring this to a close what you are asking for is simply not possible / possibly not worth the effort with PowerShell 5.1.

How to find the Windows version from the PowerShell command line

Since you have access to the .NET library, you could access the OSVersion property of the System.Environment class to get this information. For the version number, there is the Version property.

For example,

PS C:\> [System.Environment]::OSVersion.Version

Major Minor Build Revision
----- ----- ----- --------
6 1 7601 65536

Details of Windows versions can be found here.

Powershell OS check before running a command using an IF statement

Switches would be cleaner, but since you asked how to do it that specific way...

Set your variable to check against OS version (lifted from linked thread Get operating system without using WMI):

$OSVersion = (get-itemproperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name ProductName).ProductName

Construct your IF statement:

If($OSVersion -eq "Windows Server 2008 R2 Standard")
{
Write-Host "Hooray It's Server 2K8 r2!"
Invoke-Item "C:\Pictures\Hooray.jpg"
}
ElseIf($OSVersion -eq "Windows 7 Professional")
{
Write-Host "Okay, Windows 7 is cool, too!"
Invoke-Item "C:\Pictures\Smiley.jpg"
}
ElseIf($OSVersion -eq "Windows Vista")
{
Write-Host "What have I done with my life?!"
Invoke-Item "C:\Pictures\GunToHead.jpg"
}
ElseIf($OSVersion -eq "Windows Millennium Edition")
{
Write-Host "Go away, operating system. You are drunk."
Invoke-Item "C:\Pictures\LiquorAndHiccups.jpg"
}

Hope that helps. I'm assuming you're new to PowerShell, but when you get comfortable, start learning switches.

Powershell to find Server Operating system

I figured it out.

Please feel free to use it and modify it. If you have questions, let me know.

It's a simple command. Hopefully, it helps someone. This gives you the type of operating systems you have. I am filtering based on Windows Server only and computer accounts that are active. Then sort by name and select unique OS.

Get-ADComputer -Filter {(OperatingSystem -like "*windows*server*") -and (Enabled -eq "True")} -Properties OperatingSystem | Sort Name | select -Unique OperatingSystem

Output:

OperatingSystem
---------------
Windows Server 2012 R2 Standard
Windows Server 2008 R2 Standard
Windows Server 2012 R2 Standard Evaluation
Windows Server 2008 R2 Enterprise

Next command is to get all the servers and show their Operating System. Again, I am filtering based on Windows server OS and Active computer accounts. I am sorting my list by Operating system:

Get-ADComputer -Filter {(OperatingSystem -like "*windows*server*") -and (Enabled -eq "True")} -Properties OperatingSystem | sort OperatingSystem | ft DNSHostName, OperatingSystem

You can also save the above in a variable and then get the count of Servers in each operating system category:

$Servers = Get-ADComputer -Filter {(OperatingSystem -like "*windows*server*") -and (Enabled -eq "True")} -Properties OperatingSystem | Sort Name

$servers | group operatingsystem

Powershell determine the remote computer OS

DESTINATION is empty. Expanding on Keith's suggestion:

foreach ($computer in $computerlist) {
if((Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet))
{
$OS = Get-WmiObject -Computer $computer -Class Win32_OperatingSystem
if($OS.caption -like '*Windows 7*'){
$DESTINATION = $DESTINATION7
}
if($OS.caption -like '*Windows XP*'){
$DESTINATION = $DESTINATIONXP
}
}
}

This could avoid the error you're getting also. empty $DESTINATION.



Related Topics



Leave a reply



Submit