How to Preview PowerShell Scripts File in Windows Explorer Using PowerShell
Problem
Windows Explorer allows us to preview the content of a file like document, image, etc. However, it does not support all extensions, for example PowerShell.
The following example shows that Windows Explorer is able to preview html
file, but if you select PowerShell file extensions like .ps1
, .psm1
or .psd1
, it will show No preview available
.
In this blog post, we will walk you through how to enable Windows Explorer to preview PowerShell Script file using PowerShell.
Solution
To enable previewing PowerShell file, we need to add registry key value. We will do this for all PowerShell extensions which are .ps1
, .psm1
and .psd1
.
Using New-ItemProperty cmdlet
To add registry key in PowerShell, we can use New-ItemProperty
cmdlet. We will register all the keys on HKEY_CLASSES_ROOT
registry path.
The key will be .ps1
, .psm1
or .psd1
respectively. All keys will have the name as PerceivedType
and the value is text
.
New-ItemProperty -Path 'Registry::HKEY_CLASSES_ROOT\.ps1' -Name PerceivedType -PropertyType String -Value 'text'
New-ItemProperty -Path 'Registry::HKEY_CLASSES_ROOT\.psm1' -Name PerceivedType -PropertyType String -Value 'text'
New-ItemProperty -Path 'Registry::HKEY_CLASSES_ROOT\.psd1' -Name PerceivedType -PropertyType String -Value 'text'
Conclusion
To enable Windows Explorer to preview PowerShell file, we need to add registry key value. We can achieve this through PowerShell script that utilizes New-ItemProperty
cmdlet.