Ever wanted a quick and easy way to see what operating systems all your computers are running to the detail of the version number? Then wanting to know the last time they logged on?
PowerShell provides a powerful and efficient way to perform this task. In this blog post, we will show you how to use a PowerShell one-liner to search for computers in a specific OU and view their name, operating system, OS version, and last time they logged in.
Searching for computers in a specific OU
You can start by searching for computers in a specific OU. The “Get-ADComputer” cmdlet can be used to retrieve information about computer objects in Active Directory. Here is an example command that retrieves all computers in the “Computers” OU:
Get-ADComputer -SearchBase "OU=Computers,DC=example,DC=com" -Filter * -Properties Name, OperatingSystem, OperatingSystemVersion, LastLogonDate
This command uses the “-SearchBase” parameter to specify the distinguished name of the search base where the search should be conducted. In this case, we are searching in the “Computers” OU of the “example.com” domain. You should replace this with the distinguished name of the OU you want to search in.
The “-Filter” parameter is used to retrieve all computer objects in the specified OU, and the “-Properties” parameter is used to specify the properties you want to retrieve. In this case, we are retrieving the “Name”, “OperatingSystem”, “OperatingSystemVersion”, and “LastLogonDate” properties.
Formatting the output
The output of the previous command is not very user-friendly, so we need to format it to make it easier to read. We can use the “Select-Object” cmdlet to select the properties we want to display and format the output using the “Format-Table” cmdlet. Here is the final PowerShell one-liner:
Get-ADComputer -SearchBase "OU=Computers,DC=example,DC=com" -Filter * -Properties Name, OperatingSystem, OperatingSystemVersion, LastLogonDate | Select-Object Name, OperatingSystem, OperatingSystemVersion, @{Name="LastLogonDate";Expression={[DateTime]::FromFileTime($_.LastLogonDate)}} | Format-Table -AutoSize
This command retrieves all computer objects in the specified OU and selects the “Name”, “OperatingSystem”, “OperatingSystemVersion”, and “LastLogonDate” properties. The “LastLogonDate” property is converted to a readable date format using the “FromFileTime” method of the “DateTime” class. Finally, the output is formatted using the “Format-Table” cmdlet to display the information in a table format. You could even change “Format-Table” and use “Out-GridView” to give you an excel like experience where you can filter and sort columns… and to make it even more advance if you are trying to remove or disable computers, you could use the Out-GridView with a “-PassThru” parameter then pipe it to a delete or disable. ***Make sure to use -WhatIf so you don’t by accident delete all of the computers that you searched for!
Conclusion
In this blog post, we have shown you how to use a PowerShell one-liner to search for computers in a specific OU in your Active Directory and view their name, operating system, OS version, and last time they logged in. This information can be very useful for managing your domain and keeping track of your computers. This can be used for reporting and for autoamating tasks that only pertain to certain versions of Windows OS. By using PowerShell, you can quickly and easily retrieve this information and format it in a way that is easy to read and understand while using it for automations.
Leave a Reply