How to Set the Execution Policy in PowerShell

Set the execution policy in PowerShell

To set the execution policy in PowerShell for your own account, run Set-ExecutionPolicy with RemoteSigned and the CurrentUser scope. It doesn’t need administrator rights: RemoteSigned runs scripts you write and blocks unsigned scripts from the internet. Below I check the current policy, change it for one session only, deal with blocked downloads and explain each … Read more

How to List Hidden Files in PowerShell

List hidden files in PowerShell

To list hidden files in PowerShell, run Get-ChildItem with the -Hidden switch. It returns only the items that have the Hidden attribute: Use -Force instead to see hidden and normal items together. Below I show both, how to read the Mode column, how to get hidden files only, search subfolders and unhide a file. For … Read more

How to Get All Properties of an Object in PowerShell

Get all properties of an object in PowerShell

To get all properties of an object in PowerShell, pipe it to Format-List with an asterisk. You get every property name with its value: To see just the names and types, use Get-Member -MemberType Property instead. Below I compare both, loop through properties in a script, find hidden members and dig into nested objects. I … Read more

PowerShell Out-File: Syntax, Parameters and Examples

PowerShell Out-File cmdlet with examples

PowerShell Out-File sends command output to a text file, formatted exactly as it would appear in the console. Pipe any command to it and give it a path: This guide covers every parameter you’ll actually use, with tests in PowerShell 7 and 5.1. If you’re still choosing between Out-File, Set-Content and Export-Csv, start with saving … Read more

PowerShell IsNullOrEmpty vs IsNullOrWhiteSpace

PowerShell IsNullOrEmpty vs IsNullOrWhiteSpace

In PowerShell, IsNullOrEmpty vs IsNullOrWhiteSpace comes down to how they treat spaces. IsNullOrEmpty only catches $null and empty strings, while IsNullOrWhiteSpace also catches strings made only of spaces, tabs or line breaks: For user input and data from files, IsNullOrWhiteSpace is almost always the one you want. Below are both checks side by side, a … Read more

How to Find Ports in Use with PowerShell

Find ports in use with PowerShell

To find which ports are in use with PowerShell, list the listening TCP ports with Get-NetTCPConnection and add the process that owns each one: Below I find who’s using one specific port, write a quick in-use test, check UDP ports, compare with netstat and test ports on other computers. A list of my own PC’s … Read more

How to Remove Environment Variables in PowerShell

Remove environment variables in PowerShell

To remove an environment variable in PowerShell for the current session, run Remove-Item on the Env: drive: That only affects the current window. To delete a variable permanently, use the .NET method with the User or Machine scope, which I cover below. I also show a change in PowerShell 7.5 that trips up older scripts. … Read more

PowerShell Random Password Generator (Secure Methods)

PowerShell random password generator

To generate a random password in PowerShell 7.4 or later, pick characters with Get-SecureRandom, which uses a cryptographically secure generator, and join them into a string: For Windows PowerShell 5.1, or when you must guarantee every character type, use the New-RandomPassword function below. Every password in this post is a fresh random sample, so yours … 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 Check Password Expiration Date in PowerShell

Check password expiration date in PowerShell

To check a password expiration date in Active Directory with PowerShell, read the computed msDS-UserPasswordExpiryTimeComputed attribute and convert it to a date: Below I cover the attribute and the math behind it, an expiring-soon report, Microsoft 365 users, and local accounts. My test PC isn’t on a domain, so I ran the date math and … Read more