Find Users With PwdLastSet Older Than 90 Days (PowerShell)

Find users with PwdLastSet older than 90 days using PowerShell

To find users whose PwdLastSet is older than 90 days, filter Get-ADUser on PasswordLastSet, the converted version of the pwdLastSet attribute: Below I test the filter on sample accounts, handle users who must change their password, build an LDAP filter for big domains and export an audit report. My test PC isn’t on a domain, … Read more

How to Check Hard Drive Space Using PowerShell

Check hard drive space using PowerShell

To check hard drive space using PowerShell, run Get-Volume. It shows the free space (SizeRemaining) and total size of each drive, and it works without admin rights in Windows PowerShell 5.1 and PowerShell 7: Output: For a report you can reuse, including free space in GB, percent free and remote servers, query Win32_LogicalDisk. I’ll build … Read more

How to List Drives in PowerShell (Get-PSDrive, Get-Volume, CIM)

PowerShell Get-Volume listing the C drive and a recovery volume without a drive letter

To list drives in PowerShell, run Get-PSDrive -PSProvider FileSystem. It returns every disk drive letter PowerShell can see, including mapped network drives, with used and free space. For volume details such as file system and health, use Get-Volume: Below you’ll learn how to list drives in PowerShell five ways: Get-PSDrive, Get-Volume, Win32_LogicalDisk, the .NET DriveInfo … Read more

How to Create CSV Files with Headers in PowerShell

Create a CSV file with headers in PowerShell using Export-Csv

To create a CSV file with headers in PowerShell, pipe objects to Export-Csv. The header row comes from the property names, so you never type it yourself: Below I cover custom header names, header-only files, appending, empty values and the spots where PowerShell 7 and Windows PowerShell 5.1 write different files. Create a CSV file … Read more

How to Create a Self-Signed Certificate Using PowerShell

Create a self-signed certificate using PowerShell

To create a self-signed certificate using PowerShell, run New-SelfSignedCertificate with the DNS names and the certificate store to save it in. The current-user store doesn’t need admin rights: Below I show what a self-signed certificate contains, how to export it as .cer and .pfx files, and how to trust it for testing. To keep my … Read more

Get the Current Directory and Script Path in PowerShell ($PSScriptRoot)

Get the current directory and script path in PowerShell

Get-Location (or $PWD) tells you the current directory in PowerShell. For the PowerShell script path, read $PSScriptRoot for the script’s folder or $PSCommandPath for the full .ps1 path: Those two answers differ more often than you’d think. The current directory is wherever the caller happens to be, while the script path never moves. I ran … Read more

How to Get the Path of a File in PowerShell

Get the path of a file in PowerShell

To get the full path of a file in PowerShell, use Get-Item and read its FullName property. It works with a relative path too: Output: The file object also has properties for each part of the path, so you rarely need to cut the text yourself: Output: I ran every example in PowerShell 7.6 and … Read more

How to Convert Files to Base64 in PowerShell

Convert files to Base64 in PowerShell

To convert a file to Base64 in PowerShell, read its bytes with [System.IO.File]::ReadAllBytes and pass them to [System.Convert]::ToBase64String: It works the same in PowerShell 7 and 5.1. Below I encode and decode a sample file, fix the -Encoding Byte error, stream large files and compare certutil. Encode a file to Base64 Here I encode a … Read more

How to Convert a Byte Array to a String in PowerShell

Convert a byte array to a string in PowerShell

To convert a byte array to a string in PowerShell, decode it with the matching text encoding, usually UTF-8. [System.Text.Encoding]::UTF8.GetString() takes the bytes and returns the text: Output: The reverse is GetBytes(), and those two methods are most of what you need. The rest of this guide covers picking the right encoding, creating byte arrays, … Read more