Powershell Snippets

Posted on 12.10.17   Cheatsheet Powershell Scripting

List local user accounts

Get-WmiObject -Class Win32_UserAccount -Filter "LocalAccount='True'"


List local user accounts and parse array

$local_users = Get-WmiObject -Class Win32_UserAccount -Filter "LocalAccount='True'" | findstr "Caption";
for ($i = 0; $i -lt $local_users.Count; $i++) {
    Write-Host $local_users[$i].split("\\")[1];
}


List all installed Windows updates

Get-Hotfix-computername <hostname>Select HotfixID, InstalledOn |Sort-Object InstalledOn
Output will look something like:

tail equivalent in Powershell

Get-Content filename.txt -Tail 30

List all AD groups for user

Save the following code to a script called getgroups.ps1:

Param(   
    [Parameter(Mandatory=$false)][string]$username 
) 
(New-Object System.DirectoryServices.DirectorySearcher(
    "(&(objectCategory=User)(samAccountName=$($username)))"
 )).FindOne().GetDirectoryEntry().memberOf

Run the script from powershell and enter username as parameter, like this:

.\getgroups.ps1 johndoe

 See more related cheat sheets here

More on ITDB

How to debug a non starting application with ProcMon


Create a cmd alias/shortcut for issuing commands on remote computer


Turn off CTRL + ALT + DEL at login prompt Windows 10


Managing WiFi profiles in Windows 10


Fix twitchy/wobbling touchpad for Ubuntu based distributions