Script to enable preview pane for PowerShell scripts
If you are running Windows 7, you probably know what preview pane is. And if you use PowerShell and create ps1 scripts, you may also wonder how can you enable preview for PowerShell scripts in Windows Explorer.
Well, Nate Bruneau shared how to edit registry to enable preview for ps1 scripts. Being a scripter myself, I figured I would wrap it into a nice script and share it with everyone. Thanks Nate for your permission.
The scrip runs on Windows 7 and Windows Server 2008 R2. You must run it from elevated PowerShell session. The script uses elevation code from Ian Griffiths’ blog to check for elevation and exits with notification if PowerShell is not running as administrator.
Here’s the script:
#############################################################################
# Enable-ps1Preview.ps1
# This script will enable preview for ps1 files in Windows Explorer.
# Special thanks to Nate Bruneau for the idea.
#
# Created by
# Bhargav Shukla
# https://www.bhargavs.com
#
# DISCLAIMER
# ==========
# THIS CODE IS MADE AVAILABLE AS IS, WITHOUT WARRANTY OF ANY KIND. THE ENTIRE
# RISK OF THE USE OR THE RESULTS FROM THE USE OF THIS CODE REMAINS WITH THE USER.
#############################################################################
# Check if OS is Windows 7 or Windows Server 2008 R2, quit if not.
$OS = (Get-WmiObject -Class win32_OperatingSystem).caption
switch -wildcard ($OS)
{
"*Windows 7*" {"`nChecking Elevation..."}
"*Windows Server 2008 R2*" {"`nChecking Elevation..."}
default {Write-Host -ForegroundColor Red "`nYou are not running Windows 7 or Windows Server 2008 R2. You can't use this feature on older OS."; exit}
}
# Function to check if PowerShell is running elevated
function Check-Elevated
{
$wid=[System.Security.Principal.WindowsIdentity]::GetCurrent()
$prp=new-object System.Security.Principal.WindowsPrincipal($wid)
$adm=[System.Security.Principal.WindowsBuiltInRole]::Administrator
$IsAdmin=$prp.IsInRole($adm)
if ($IsAdmin)
{
Set-Variable -Name elevated -Value $true -Scope 1
}
}
# Make registry changes if running elevated, throw error if not
Check-Elevated
If ($elevated -eq $true)
{
# Set Registry Key variables
$REG_KEY = ".ps1"
# Open remote registry
$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('ClassesRoot', ".")
# Open the targeted remote registry key/subkey as read/write
$regKey = $reg.OpenSubKey($REG_KEY,$true)
# Set PerceivedType to "text"
if ($regKey -ne $null)
{
$regKey.Setvalue('PerceivedType', 'text', 'String')
$regKey.Setvalue('Content Type', 'text/plain', 'String')
# Close the Reg Key
$regKey.Close()
Write-Host -ForegroundColor Green -BackgroundColor Black "Preview for .ps1 files is now enabled. Enable preview pane in Windows Explorer.`n"
}
}
else
{
Write-Host -ForegroundColor Red -BackgroundColor Black "Please run PowerShell as administrator before you run this script.`n"
}
You can download the script here – Enable-ps1Preview.ps1
Comments
- Anonymous
November 04, 2011
cool! in a minute I had ps1 preview on my server 2008, very helpful, thank you!