PowerShellISE-preview.psm1

$PSModule = $ExecutionContext.SessionState.Module
$PSModuleRoot = $PSModule.ModuleBase

function Update-ISEPreviewShortcut($wshell, $shortcutPath, $exeName)
{
    $shortcut = $wshell.CreateShortcut($shortcutPath)
    if ($shortcut.TargetPath -ne $shortcutPath)
    {
        $shortcut.TargetPath = "$PSModuleRoot\$exeName"
        $shortcut.Description = "Windows PowerShell Integrated Scripting Environment. Performs object-based (command-line) functions"
        $shortcut.WorkingDirectory = "%HOMEDRIVE%%HOMEPATH%"
        $shortcut.Save()
    }
}

function Update-ISEPreviewShortcuts([switch]$OnlyUpdateExisting)
{
    $startMenuRootPath = [System.Environment]::GetFolderPath('StartMenu')
    $powerShellShortcutPath = [System.IO.Path]::Combine($startMenuRootPath, "Programs\Windows PowerShell")
    $isePreviewShortcutPath = [System.IO.Path]::Combine($powerShellShortcutPath, "Windows PowerShell ISE Preview.lnk")
    $isePreviewShortcutX86Path = [System.IO.Path]::Combine($powerShellShortcutPath, "Windows PowerShell ISE Preview (x86).lnk")

    $wshell = New-Object -ComObject WScript.Shell

    if (!$OnlyUpdateExisting)
    {
        # If the shortcut folder doesn't exist, create it
        New-Item -ItemType Directory -Force -Path $powerShellShortcutPath | Out-Null
    }

    if (!$OnlyUpdateExisting.IsPresent -or [System.IO.File]::Exists($isePreviewShortcutPath))
    {
        Update-ISEPreviewShortcut $wshell $isePreviewShortcutPath "powershell_ise.exe"
    }

    if (!$OnlyUpdateExisting.IsPresent -or [System.IO.File]::Exists($isePreviewShortcutPath))
    {
        Update-ISEPreviewShortcut $wshell $isePreviewShortcutX86Path "powershell_ise.x86.exe"
    }
}

function Install-ISEPreviewShortcut
{
    # Call the update cmdlet without passing the -OnlyUpdateExisting parameter
    Update-ISEPreviewShortcuts
}

function Start-ISEPreview
{
    # Run the included binary, passing along the given arguments
    & "$PSModuleRoot\powershell_ise.exe" @args
}

# Set up the 'isep' alias
Set-Alias isep Start-ISEPreview -Option AllScope -Scope Global
Set-Alias Install-ISEPreviewShortcuts Install-ISEPreviewShortcut

# Ensure that existing ISE Preview shortcuts have the right path
Update-ISEPreviewShortcuts -OnlyUpdateExisting