How to List Directories and Files in PowerShell

List directories and files in PowerShell

To list directories and files in PowerShell, use Get-ChildItem. On its own, it lists every folder and file in the current location. Add -Directory for folders only, -File for files only and -Recurse to include subfolders. Here it is on a sample Projects folder I created for this article: Output: The Mode column tells you … Read more

How to Check Who Modified a File Last with PowerShell

Check who modified a file last in Windows with PowerShell

Windows doesn’t record who last modified a file. NTFS keeps the time of the last change, but not the user, so to check who modified a file with PowerShell you need file auditing turned on before the change happens. Below I show what you can get right away (the time, the owner and Office’s own … Read more

PowerShell Script to Delete Files Older Than 30 Days

PowerShell delete files older than 30 days

To delete files older than 30 days with PowerShell, list the files with Get-ChildItem, keep the ones whose LastWriteTime is before a cutoff date, and pipe them to Remove-Item. Run it with -WhatIf first: Output: In a real script, set the cutoff with (Get-Date).AddDays(-30). I used a fixed date and a sample folder so the … Read more

PowerShell: Find Files Modified After a Date (or in the Last 24 Hours)

PowerShell find files modified after a date

To find files modified after a date in PowerShell, pipe Get-ChildItem -File -Recurse to Where-Object and compare LastWriteTime with the date: Output: LastWriteTime is the “Date modified” column in File Explorer. I used a sample folder with fixed dates, and I ran every example in PowerShell 7.6 and Windows PowerShell 5.1, with the same results. … 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

Fix “Running Scripts Is Disabled on This System” in PowerShell

Fix PowerShell running scripts is disabled on this system

To fix “running scripts is disabled on this system” in PowerShell, set the execution policy to RemoteSigned for your account. It doesn’t need admin rights: The error means the effective policy is Restricted, which is the default on Windows client PCs. Below I reproduce it, check every policy scope, and show fixes from a single … Read more

How to Get the Type of an Object in PowerShell

Get the type of an object in PowerShell

To get the type of an object or variable in PowerShell, call its GetType() method. .Name gives the short name and .FullName the full .NET name: Output: To test for a type instead of reading it, use the -is operator, covered below. I ran every example in PowerShell 7.6 and Windows PowerShell 5.1, with the … Read more

PowerShell Round to 2 Decimal Places (and Whole Numbers)

PowerShell round to 2 decimal places

To round a number to 2 decimal places in PowerShell, use [Math]::Round($number, 2). To display it with exactly two decimals, use the format operator, ‘{0:N2}’ -f $number, or .ToString(‘0.00′): Output: They look the same here, but they don’t return the same kind of value, and they don’t always round the same way. I ran every … Read more

Measure-Object in PowerShell: Sum, Average, Max and Count

Measure-Object in PowerShell

Measure-Object calculates the count, sum, average, maximum and minimum of numbers or of a property. Pipe the values to it and pick the statistics you want: Output in Windows PowerShell 5.1: PowerShell 7 shows the same numbers plus an empty StandardDeviation row. I ran every example in PowerShell 7.6 and Windows PowerShell 5.1 and note … Read more

How to Pass Variables to a PowerShell Script

Pass variables to a PowerShell script

To pass variables to a PowerShell script, declare them in a param() block at the top of the script, then pass values by name when you run it: Every example below uses one sample script, so you can copy it and follow along. I tested each call in PowerShell 7 and Windows PowerShell 5.1. Declare … Read more