Amazing PowerShell One-Liners for System Administrators
Here is a collection of some really cool PowerShell one-liners scripts. I find them very useful for day to day system administration tasks. I will keep adding to this list as I learn more useful commands.
List of all installed applications on a Windows device
This one-liner searches the Windows Registry to identify installed applications. It will display the version, publisher, and install date in a formatted table. You can output to a text file by simply adding > output.txt
This one-liner will also list all installed Windows Update files (KB)
Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Format-Table –AutoSize
Powershell Output
Here is the Powershell output:

Get all installed KB numbers from Windows Update
Similar to the first one-liner, this script will only return the installed KB patches on a Windows device
$wu = new-object -com “Microsoft.Update.Searcher”
$totalupdates = $wu.GetTotalHistoryCount()
$all = $wu.QueryHistory(0,$totalupdates)
$OutputCollection= @()
Foreach ($update in $all)
{
$string = $update.title
$Regex = “KB\d*”
$KB = $string | Select-String -Pattern $regex | Select-Object {$_.Matches }
$output = New-Object -TypeName PSobject
$output | add-member NoteProperty “HotFixID” -value $KB.‘ $_.Matches ‘.Value
$output | add-member NoteProperty “Title” -value $string
$OutputCollection += $output
}
$OutputCollection | Sort-Object HotFixID | Format-Table -AutoSize
Write-Host “$($OutputCollection.Count) Updates Found”
Powershell Output
Here is the Powershell output:

Find Admin Shares on my computer
This great little oneline uses RegEx to find local admin shares
Gwmi Win32_Share|%{"\\$($_|% P*e)\$($_.Name)"}
Powershell Output
Here is the Powershell output:

Find Scheduled tasks that are running
Useful if you want to know what a server tasks are
(get-scheduledtask).<strong>where</strong>({$_.state -eq 'running'})
Powershell Output
Here is the Powershell output:

Find files
Find any file on a filesystem
This script will look for the host.bak file in C:\Window, the account I have used does not have permissions to view everything in C:\Windows, there i use -ErrorAction SilentlyContinue to make the output much easier to read
Get-Childitem -Path C:\windows -Recurse -Filter <em>hosts.bak</em> -ErrorAction SilentlyContinue
Powershell Output
Here is the Powershell output:


This script will recursively search the path C:\Temp for all filenames containing .log
Get-ChildItem -path C:\temp\ -Recurse -Filter <em>.log</em>

Find Last Bootup Time
This is a really useful one-liner that can be run directly from the server, or you can run it against multiple servers
Get-WmiObject win32_operatingsystem |select @{Name="Last Boot Time"; Expression={$_.ConvertToDateTime($_.LastBootUpTime)}}, PSComputerName
Use the property -computername to target other nodes

Free Disk space information
This great little one liner will give you the disk space of your local machine in a value and percentage. You can also pipe computer names to the one-liner so that multiple computers can be checked
gwmi Win32_LogicalDisk -Filter "DeviceID='C:'" | Select Name, FileSystem,FreeSpace,BlockSize,Size | % {$_.BlockSize=(($_.FreeSpace)/($_.Size))*100;$_.FreeSpace=($_.FreeSpace/1GB);$_.Size=($_.Size/1GB);$_}| Format-Table Name, @{n='FS';e={$_.FileSystem}},@{n='Free, Gb';e={'{0:N2}'-f $_.FreeSpace}}, @{n='Free,%';e={'{0:N2}'-f $_.BlockSize}} -AutoSize

or you can use this:
Get-PSDrive -PSProvider filesystem | where-object {$_.used -gt 0} |select-Object -property Root,@{name="SizeGB";expression={($_.used+$_.free)/1GB -as [int]}}, @{name="UsedGB";expression={($_.used/1GB) -as [int]}}, @{name="FreeGB";expression={($_.free/1GB) -as [int]}}, @{name="PctFree";expression={[math]::round(($_.free/($_.used+$_.free))*100,2)}}

Find out how big a folder is
This useful oneliner will tell you how big the temp folder is, including the biggest file, average size and total size
dir -path C:\Scripts -file -recurse -force | measure-object length -sum -max -average | Select-Object @{name="Total Files";Expression={$_.count}},@{name="Largest File(MB)";Expression={"{0:F2}" -f ($_.maximum/1MB)}},@{name="Average Size(MB)";Expression={"{0:F2}" -f ($_.average/1MB)}},@{name="Total Size(MB)";Expression={"{0:F2}" -f ($_.sum/1MB)}}

Active Directory Bulk Add Users
See the attached Video I uploaded to YouTube that goes into detail about bulk adding AD-Users
These one-liner can require RSAT directory services installed locally, or run them on a Domain Controller.
I have also written a deep dive on PowerShell Active Directory. Check it out here
Recent Comments