PowerShell Regex: How to Use Regular Expressions (With Examples)

PowerShell regex examples

PowerShell regex (regular expressions) lets you match, extract, replace and split text by pattern. The operators -match, -replace and -split all take a regex, and $Matches holds what -match found: Output: Below I cover case sensitivity, named groups, replacing, splitting, finding every match, Select-String and switch -Regex. Every example ran in PowerShell 7.6 and 5.1, … Read more

PowerShell Not Equal Operator (-ne) With Examples

PowerShell not equal operator

The not equal operator in PowerShell is -ne. It returns True when two values are different, and it ignores case for text: Output: PowerShell doesn’t support !=. Writing if (5 != 3) fails with “Unexpected token ‘!=’ in expression or statement.” I ran every example in PowerShell 7.6 and Windows PowerShell 5.1, with the same … Read more

Fix: The File Is Not Digitally Signed. You Cannot Run This Script

Fix the file is not digitally signed error in PowerShell

The “file is not digitally signed. You cannot run this script on the current system” error means your execution policy only runs signed scripts, and this one isn’t signed. For a script you trust, the quickest fix is to unblock it: Below I reproduce the error, show why it happens, and walk through four fixes … Read more

Delete User Profiles Older Than 30 Days with PowerShell

Delete user profiles older than 30 days using PowerShell

To delete user profiles older than 30 days with PowerShell, find them with Win32_UserProfile, skip system and loaded profiles, and remove each one with Remove-CimInstance. Run it as administrator, with -WhatIf first: Remove-CimInstance deletes the profile folder and its registry entry together. Below I explain each filter, test it on sample data, and cover remote … Read more

How to Delete User Profiles Using PowerShell in Windows 11

Delete user profiles using PowerShell in Windows 11

To delete user profiles using PowerShell in Windows 11, find the profile with Get-CimInstance Win32_UserProfile and pipe it to Remove-CimInstance in an elevated window. That removes the folder and the registry entry together: Deleting a profile can’t be undone, so below I list profiles safely, find stale ones and preview the removal with -WhatIf. I … Read more

How to Open PowerShell in a Folder (Windows 11 and 10)

Open PowerShell in a folder

To open PowerShell in a folder, open the folder in File Explorer, click the address bar, type pwsh (or powershell for Windows PowerShell 5.1) and press Enter. PowerShell starts with that folder as its location. In Windows 11 you can also right-click inside the folder and choose Open in Terminal. Below I cover shortcuts, command-line … Read more

How to List Local Administrators Using PowerShell

List local administrators using PowerShell

To list local administrators with PowerShell, run Get-LocalGroupMember on the Administrators group. It works without admin rights in PowerShell 7 and 5.1: Below I run it, make it work on any language of Windows, use net localgroup as a fallback, check remote PCs and explain why an admin account isn’t always running as admin. List … Read more

How to Get Windows Activation Status Using PowerShell

Get Windows activation status using PowerShell

To get the Windows activation status with PowerShell, query the SoftwareLicensingProduct class for Windows and read LicenseStatus. A value of 1 means Windows is activated: It works without admin rights in PowerShell 7 and 5.1. Below I turn the number into readable text, build a quick yes-or-no check, query remote PCs and compare it with … Read more

How to Get Windows Event Logs Using PowerShell

Get Windows event logs using PowerShell

To get Windows event logs using PowerShell, run Get-WinEvent with the log name. This returns the 20 newest events from the System log: For anything bigger, filter with -FilterHashtable so Windows does the filtering before PowerShell sees the events. Below I cover listing logs, filtering by ID, level and date, the Security log, exporting and … Read more

How to Set a Proxy in PowerShell (7 and 5.1)

Set a proxy in PowerShell

To set a proxy in PowerShell for one request, pass it to -Proxy on Invoke-WebRequest or Invoke-RestMethod. Add -ProxyUseDefaultCredentials if the proxy needs your Windows sign-in: For a whole session, the method differs between PowerShell 7 and Windows PowerShell 5.1. I cover both below, plus bypass lists and the Windows-wide proxy. The proxy server in … Read more