Pre-Loading the Get-WindowsServers function from your PWSH or ISE profile is real handy for inventorys
# Function to get Windows Servers
function Get-WindowsServers {
param (
[string]$Domain
)
if(!($Domain)){$Domain = "Burwelltech"}
# Get all Windows Server computers in the specified domain
$servers = Get-ADComputer -Filter {(OperatingSystem -like "*Windows Server*") -and (Enabled -eq "true")} -Server $Domain -Properties * |select Name,operatingsystem,DNSHostName,DistinguishedName,Enabled
return $servers
}
Then you can just sort by the field you need like this:
Get-WindowsServers |sort -Descending -Property OperatingSystem |ft -AutoSize
As you can see, the $Domain has to be set (like to the default production domain netBIOS name) in order to function, if you don’t enter one at the prompt
Get-WindowsServers -Domain AnotherClientDomain |sort -Descending -Property OperatingSystem |ft -AutoSize
If you don’t want to preset the Domain variable you can then call this function rom other scripts with the double dot:
. .\get-windowsservers



Leave a Reply