Access to the Path Is Denied in PowerShell: Causes and Fixes

Fix access to the path is denied in PowerShell

“Access to the path is denied” in PowerShell means Windows refused the file operation, usually because of a read-only file, a lock held by another program, an NTFS deny rule or a folder path used as a file. For read-only files, -Force fixes it: The trick is figuring out which cause you’ve hit, because each … Read more

How to Run a PowerShell Script in Visual Studio Code

Run a PowerShell script in Visual Studio Code

To run a PowerShell script in Visual Studio Code, install Microsoft’s PowerShell extension, open your .ps1 file and press F5. To run only a few lines, select them and press F8. You can also type the script path in the terminal: Below I set up the extension, run a script with and without parameters, debug … Read more

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 Find Unquoted Service Paths with PowerShell (and Fix Them)

Find unquoted service paths with PowerShell

To find an unquoted service path with PowerShell, read every service’s PathName from Win32_Service and flag the ones that don’t start with a quote but have a space inside the .exe part: It’s a defensive audit for machines you manage. Below I run it for real in PowerShell 7 and 5.1, test the regex on … Read more

Convert Canonical Name to Distinguished Name in PowerShell

Convert a canonical name to a distinguished name in PowerShell

To convert a canonical name to a distinguished name in PowerShell, split the path on slashes, turn the domain into DC= parts, and add the folders in reverse order. One line handles a simple path: Real paths need more care. Below is a reusable function that handles any depth, the built-in Users container and commas … Read more

PowerShell Out-GridView: PassThru, OutputMode, Wait and Filters

PowerShell Out-GridView interactive grid window

PowerShell Out-GridView sends command output to an interactive table in its own window, where you can sort, filter and pick rows instead of scrolling the console. Pipe any command to it: Add -PassThru and the rows you select come back into the pipeline, which turns the grid into a quick picker for scripts. I ran … Read more

PowerShell Write-Host Color: All 16 Colors and Examples

PowerShell Write-Host color examples with ForegroundColor, BackgroundColor and PSStyle

To change the Write-Host color in PowerShell, add -ForegroundColor for the text and -BackgroundColor for the area behind it: Both parameters take one of 16 console color names. Below I cover the full list, several colors on one line, a reusable status function, and the ANSI colors that only PowerShell 7 understands. Write-Host -ForegroundColor and … Read more

How to Show Progress When Copying Files with PowerShell

Show a progress bar when copying files with PowerShell Write-Progress and Robocopy

To show progress when copying files with PowerShell, loop over the files and call Write-Progress before each Copy-Item. The bar’s percentage is the file number divided by the file count: PowerShell 7.4 and later also give Copy-Item a progress bar of its own. Windows PowerShell 5.1 doesn’t have one, so I’ll show both routes, plus … Read more