{"id":578,"date":"2021-12-13T06:56:44","date_gmt":"2021-12-13T14:56:44","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/powershell-community\/?p=578"},"modified":"2021-12-13T06:56:44","modified_gmt":"2021-12-13T14:56:44","slug":"how-to-preview-powershell-scripts-in-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell-community\/how-to-preview-powershell-scripts-in-powershell\/","title":{"rendered":"How to Preview PowerShell Scripts In PowerShell"},"content":{"rendered":"<p><strong>Q:<\/strong> When I use Windows Explorer and select a PowerShell script file &#8211; I do not see the script in the preview window. Can I fix that?<\/p>\n<p><strong>A:<\/strong> You can make a few simple registry updates and do just what you want!<\/p>\n<p>At some time in the deep and distant past, Windows Explorer gained the preview pane feature. The idea is simple: you select a file in Explorer and Windows shows you a preview of the file in a separate pane. I love this feature, although when clicking on a Word document, it could take a few moments before I could view. And if you are viewing a file, it was &#8220;open&#8221; and you could not delete it in a separate window! A great feature albeit with some minor side effects &#8211; so it makes sense that this is turned off by default. But you can easily turn it back on!<\/p>\n<h2>Microsoft PowerToys to the rescue??<\/h2>\n<p>You can use <a href=\"https:\/\/docs.microsoft.com\/windows\/powertoys\">Microsoft&#8217;s Power Toys for Windows<\/a> to enable Explorer to preview more file types. I love these tools &#8211; and have them installed on my computers. Sadly, PowerToys currently does not enable previewing of PowerShell files.<\/p>\n<h2>Enabling Preview in Windows Explorer<\/h2>\n<p>As I mentioned above, file preview within Windows Explorer is disabled by default. To turn this on, use Explorer&#8217;s View menu and select preview. I leave the details of how to set this up as an exercise for the user. As a small aside, this setting gets reset each time you upgrade Windows &#8211; as a Windows Insider, I have to reset this with each new build I take. \ud83d\ude41<\/p>\n<h2>Enabling Preview of .PS1\/.PSD1\/.PSM1 files<\/h2>\n<p>Once you enable preview mode in Explorer as shown above, when you select a <code>.PS1<\/code> file &#8211; you see something like this:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-content\/uploads\/sites\/69\/2021\/12\/before.png\" alt=\"Viewing a .PS1 file in Preview\" \/><\/p>\n<p>There is currently no mechanism in Explorer to change the list of file types to be displayed. Fortunately, there is a straightforward mechanism that involves setting a registry key value. To enable Explorer to display the relevant files, you can use the following script fragment:<\/p>\n<pre><code class=\"language-powershell\"># Set path variables for PowerShell file types\n$Path1 = 'Registry::HKEY_CLASSES_ROOT\\.ps1'\n$Path2 = 'Registry::HKEY_CLASSES_ROOT\\.psm1'\n$Path3 = 'Registry::HKEY_CLASSES_ROOT\\.psd1'\n\n# Enable preview of those file types\nNew-ItemProperty -Path $Path1 -Name PerceivedType -PropertyType String  -Value 'text'\nNew-ItemProperty -Path $Path2 -Name PerceivedType -PropertyType String  -Value 'text'\nNew-ItemProperty -Path $Path3 -Name PerceivedType -PropertyType String  -Value 'text'<\/code><\/pre>\n<h2>Result!<\/h2>\n<p>Once you run this script, Explorer displays the script file, Explorer now looks like this:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-content\/uploads\/sites\/69\/2021\/12\/after.png\" alt=\"Viewing a .PS1 file in Preview after updating the registry\" \/><\/p>\n<p>That&#8217;s it &#8211; a small change to the registry and I can now preview PowerShell files. Very handy!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Q: When I use Windows Explorer and select a PowerShell script file &#8211; I do not see the script in the preview window. Can I fix that? A: You can make a few simple registry updates and do just what you want! At some time in the deep and distant past, Windows Explorer gained the [&hellip;]<\/p>\n","protected":false},"author":4034,"featured_media":77,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[13],"tags":[61,62],"class_list":["post-578","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-explorer","tag-explorer-preview"],"acf":[],"blog_post_summary":"<p>Q: When I use Windows Explorer and select a PowerShell script file &#8211; I do not see the script in the preview window. Can I fix that? A: You can make a few simple registry updates and do just what you want! At some time in the deep and distant past, Windows Explorer gained the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/posts\/578","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/users\/4034"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/comments?post=578"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/posts\/578\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/media\/77"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/media?parent=578"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/categories?post=578"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell-community\/wp-json\/wp\/v2\/tags?post=578"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}