Skip to content

Instantly share code, notes, and snippets.

@JustinGrote
Last active May 24, 2023 05:56
Show Gist options
  • Save JustinGrote/dc10c6120007dceba2da1825ace6b073 to your computer and use it in GitHub Desktop.
Save JustinGrote/dc10c6120007dceba2da1825ace6b073 to your computer and use it in GitHub Desktop.
Install the PSReadline Autocomplete Beta
#requires -version 5
<#
.EXAMPLE
pwsh.exe -noni -nop -c "iwr -useb https://git.io/InstallPSReadlineAutocompleteBeta.ps1 | iex"
#>
param (
#Path to Install the Module. Will default to the user module folder
$ModuleInstallPath,
$TempZipPath = [io.path]::ChangeExtension([io.path]::GetTempFileName(), '.zip'),
#Change this if your vscode isn't installed in a typical location
[String[]]$vsCodeDataFolder = (Resolve-Path -Path ([IO.Path]::Combine(
$HOME,
".vscode*",
"extensions",
"ms-vscode.powershell-*",
"modules"
))),
$NewVersion = '2.99.99',
$ModuleName = 'PSReadLine',
$PSReadlineDownloadUri = 'https://github.com/PowerShell/PSReadLine/files/4396121/PSReadLine.zip',
#If specified, don't try to detect vscode and update it
[Switch]$SkipVSCode
)
$ErrorActionPreference = 'Stop'
if (Get-Module -Name 'PSReadLine') {
throw 'PSReadline detected as running. You must run this where psreadline has not been loaded. Hint: pwsh -noninteractive -noprofile -c'
}
function GetUserModuleFolder {
$myPSEdition = if ($PSEdition -eq 'Core') {'PowerShell'} else {'WindowsPowerShell'}
if ($isLinux -or $isMacOS) {
[IO.Path]::Combine([environment]::GetFolderPath('MyDocuments'),'.local','share',$myPSEdition.tolower(),'Modules')
} else {
[IO.Path]::Combine([environment]::GetFolderPath('MyDocuments'),$myPSEdition,'Modules')
}
}
if (-not $ModuleInstallPath) {$ModuleInstallPath = GetUserModuleFolder}
Write-Host -nonewline -fore Cyan 'Downloading experimental PSReadline version...'
#IWR's write-progress slows down the downoad on some versions of Powershell
([net.webclient]::new()).DownloadFile(
$PSReadlineDownloadUri, #URI
$TempZipPath #Destination Path
)
Write-Host -fore Green ' OK'
$psReadlinePath = [IO.Path]::Combine($moduleInstallPath,$ModuleName,$newVersion)
if (test-path $psreadlinepath) {
write-warning 'PSReadline Experimental already detected. Deleting...'
try {
Remove-Item -Force -Recurse -Path $psreadlinePath
} catch [UnauthorizedAccessException] {
throw 'PSReadline experimental detected as in use in another powershell session. Please close all powershell sessions before running this script'
}
}
#Extract the folder and place it in the correct location
$psReadlineBase = (Split-Path $PSReadlinePath)
$psReadlineExtractPath = Join-Path $psReadlineBase $moduleName
Expand-Archive -Path $TempZipPath -DestinationPath $psReadlineBase -Force
#Note: PrivateData is set to something to clear the prerelease tag, due to a PSReadline issue
Update-ModuleManifest -Path (Join-Path $psReadlineExtractPath "$moduleName.psd1") -PrivateData @{PSReadlineBeta = $true} -ModuleVersion $NewVersion
Move-Item $psReadlineExtractPath $psreadlinePath
if (-not $SkipVSCode) {
$vsCodeDataFolder.foreach{
$moduleDestination = Join-Path $PSItem $moduleName
write-host -NoNewline -fore cyan "Detected $moduleDestination. Installing..."
Remove-Item -Recurse -Force -Path $moduleDestination -ErrorAction SilentlyContinue
if ($isLinux -or $isMacOS) {$linkType = 'SymbolicLink'} else {$linkType = 'Junction'}
New-Item -ItemType $linkType -Path $moduleDestination -Target $psreadlinePath > $null
Write-Host -fore Green ' OK'
}
}
Write-Host -fore Green "Install complete! You need to reload your powershell session to apply the changes."
Write-Host -fore Cyan "You should also add this to your profile for the best experience:
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward
"
Write-Warning "This script installs the module as version 2.99.99, so any PSReadline regular updates you do will be overridden unless you remove it"
""
@JustinGrote
Copy link
Author

The module is available on Powershell Gallery now so this script is no longer necessary, just install-module psreadline -allowprerelease

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment