When you view PowerShell scripts in Windows Explorer and have the preview pane open, by default you don’t get a code preview for your script files. The preview pane remains blank.

To enable the preview, simply use the function below:

function Enable-PowerShellFilePreview
{
    [CmdletBinding()]
    param
    (
        [string]
        $Font = 'Courier New',
        
        [int]
        $FontSize = 60
    )
    
    # set the font and size (also applies to Notepad)
    $path = "HKCU:\Software\Microsoft\Notepad"
    Set-ItemProperty -Path $path -Name lfFaceName -Value $Font
    Set-ItemProperty -Path $path -Name iPointSize -Value $FontSize
    
    # enable the preview of PowerShell files
    $path = 'HKCU:\Software\Classes\.ps1'
    $exists = Test-Path -Path $path
    if (!$exists){
        $null = New-Item -Path $Path
    }
    $path = 'HKCU:\Software\Classes\.psd1'
    $exists = Test-Path -Path $path
    if (!$exists){
        $null = New-Item -Path $Path
    }
    
    $path = 'HKCU:\Software\Classes\.psm1'
    $exists = Test-Path -Path $path
    if (!$exists){
        $null = New-Item -Path $Path
    }

    
    Get-Item HKCU:\Software\Classes\* -Include .ps1,.psm1,.psd1 | Set-ItemProperty -Name PerceivedType -Value text
}

Once you run the function, use this command:

 
PS> Enable-PowerShellFilePreview
 

If you like, you can also set a font family and size for the preview. Note that this setting is shared with the Notepad:

 
PS> Enable-PowerShellFilePreview -Font Consolas -FontSize 100
 

No restart is required to see the effect. Just make sure the preview pane is visible in Windows Explorer, and select a PowerShell file.


psconf.eu – PowerShell Conference EU 2019 – June 4-7, Hannover Germany – visit www.psconf.eu There aren’t too many trainings around for experienced PowerShell scripters where you really still learn something new. But there’s one place you don’t want to miss: PowerShell Conference EU - with 40 renown international speakers including PowerShell team members and MVPs, plus 350 professional and creative PowerShell scripters. Registration is open at www.psconf.eu, and the full 3-track 4-days agenda becomes available soon. Once a year it’s just a smart move to come together, update know-how, learn about security and mitigations, and bring home fresh ideas and authoritative guidance. We’d sure love to see and hear from you!

Twitter This Tip! ReTweet this Tip!

Anonymous
  • I solved this with a foreach loop:

    #setup explorer preview for PowerShell and text files
    $FileTypes = @(".ps1",".psm1",".txt",".log",".sh",".json",".yml",".vbs") #.vbs
    foreach ($ft in $FileTypes)
    {
        $RegLoc = Join-Path -Path 'HKCU:\Software\Classes' -ChildPath $ft
        if (!(Test-Path -Path $RegLoc))
        {
            New-Item -Path "HKCU:\Software\Classes" -Name $ft
        }
        Set-ItemProperty -Path $RegLoc -Name 'PerceivedType' -Value 'text' -Force
    }

  • Windows Explorer on Windows 8 and higher already does this by default since they RTM'd - I've been using this for years now. It also show previews of any other text file and office docs, pdf, and pictures. No reg hacking needed.