How to Convert XML to a Table in PowerShell (With Examples)

Convert XML to a table in PowerShell

To convert XML to a table in PowerShell, cast the file to [xml], walk down to the repeating element with dot notation, and pipe those nodes to Format-Table. Each child element and attribute becomes a column: That covers flat files. Real XML has attributes, nested nodes and namespaces, so I’ll also show how to pick … Read more

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 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

How to Join Arrays in PowerShell (+, -join, Join-String)

Join arrays in PowerShell with the plus operator and -join

To join arrays in PowerShell, use the plus operator. It returns a new array with every item from the first array followed by every item from the second: If you want one string instead of one array, use $all -join ‘, ‘. I’ll cover both below, plus the traps I hit when one side holds … Read more

How to Convert an Object to an Array in PowerShell

Convert an object to an array in PowerShell with the @() operator

To convert an object to an array in PowerShell, wrap it in the array subexpression operator @(). You always get an array back, even for one object or no objects at all: What “object” means changes the answer, though. Below I cover single objects, PSCustomObject properties, hashtables, command output, and JSON, with the spots where … Read more