Monday, July 22, 2024
HomePowershellEasy methods to Preview PowerShell Scripts In PowerShell

Easy methods to Preview PowerShell Scripts In PowerShell

Q: After I use Home windows Explorer and choose a PowerShell script file – I don’t see the script within the preview window. Can I repair that?

A: You may make a couple of easy registry updates and do exactly what you need!

At a while within the deep and distant previous, Home windows Explorer gained the preview pane function. The thought is straightforward: you choose a file in Explorer and Home windows exhibits you a preview of the file in a separate pane. I like this function, though when clicking on a Phrase doc, it might take a couple of moments earlier than I might view. And in case you are viewing a file, it was “open” and you may not delete it in a separate window! An important function albeit with some minor unintended effects – so it is smart that that is turned off by default. However you may simply flip it again on!

Microsoft PowerToys to the rescue??

You need to use Microsoft’s Energy Toys for Home windows to allow Explorer to preview extra file varieties. I like these instruments – and have them put in on my computer systems. Sadly, PowerToys at present doesn’t allow previewing of PowerShell recordsdata.

Enabling Preview in Home windows Explorer

As I discussed above, file preview inside Home windows Explorer is disabled by default. To show this on, use Explorer’s View menu and choose preview. I go away the small print of learn how to set this up as an train for the person. As a small apart, this setting will get reset every time you improve Home windows – as a Home windows Insider, I’ve to reset this with every new construct I take. 🙁

Enabling Preview of .PS1/.PSD1/.PSM1 recordsdata

When you allow preview mode in Explorer as proven above, when you choose a .PS1 file – you see one thing like this:

There’s at present no mechanism in Explorer to vary the checklist of file varieties to be displayed. Happily, there’s a simple mechanism that includes setting a registry key worth. To allow Explorer to show the related recordsdata, you should utilize the next script fragment:

# Set path variables for PowerShell file varieties
$Path1 = 'Registry::HKEY_CLASSES_ROOT.ps1'
$Path2 = 'Registry::HKEY_CLASSES_ROOT.psm1'
$Path3 = 'Registry::HKEY_CLASSES_ROOT.psd1'

# Allow preview of these file varieties
New-ItemProperty -Path $Path1 -Identify PerceivedType -PropertyType String  -Worth 'textual content'
New-ItemProperty -Path $Path2 -Identify PerceivedType -PropertyType String  -Worth 'textual content'
New-ItemProperty -Path $Path3 -Identify PerceivedType -PropertyType String  -Worth 'textual content'


When you run this script, Explorer shows the script file, Explorer now seems like this:

Viewing a .PS1 file in Preview after updating the registry

That’s it – a small change to the registry and I can now preview PowerShell recordsdata. Very useful!



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments