Thursday, December 09, 2021

Viewing PowerShell Files in Preview within WIndows Explorer

Windows Explorer has a nice feature that shows the contents of a selected file. Click on a file and in the right pane you can see the contents. This is a great feature, except it does not work for ALL filetypes out of the box. So would you wish to view a .PS1 file this way - you are out of luck. By default

By default, the preview pane is disabled. So you must first enable it. With the latest versions of Windows 11, it looks like this:


But even after enabling this, you still can not view .PS1 files (or .PSM1 or .PSD1 files either) in the preview pane within Explorer.  

But like just about every default - with PowerShell there is usually a way to override it. And it turns out to make these files visible in the preview pane, you just need to run the following script:

# Define paths
$Path1 = 'Registry::HKEY_CLASSES_ROOT\.ps1'
$Path2 = 'Registry::HKEY_CLASSES_ROOT\.psm1'
$Path3 = 'Registry::HKEY_CLASSES_ROOT\.psd1'

# Set registry values to enable preview
New-ItemProperty -Path $Path1 -Name PerceivedType -PropertyType String  -Value 'text'
New-ItemProperty -Path $Path2 -Name PerceivedType -PropertyType String  -Value 'text'
New-ItemProperty -Path $Path3 -Name PerceivedType -PropertyType String  -Value 'text'

That's it. Once you run the script, you see things like this:


Another mystery solved