Skip to content

Instantly share code, notes, and snippets.

@milnak
Last active August 15, 2024 18:09
Show Gist options
  • Save milnak/255729e1e8bb8f69bc9901374313ae47 to your computer and use it in GitHub Desktop.
Save milnak/255729e1e8bb8f69bc9901374313ae47 to your computer and use it in GitHub Desktop.
Launch vscode with wildcards: PowerShell script to launch vscode, supporting wildcards
# Launch vscode with wildcards
function Get-UninstallPath {
Param(
[Parameter(Mandatory=$true)][string]$ProductId
)
$regPath = "/SOFTWARE/Microsoft/Windows/CurrentVersion/Uninstall/$ProductId"
# Check user location first
$path = Get-ItemProperty -Path "HKCU:$regPath" -Name 'InstallLocation' -ErrorAction SilentlyContinue
if (-not $path) {
# Check system location next
$path = Get-ItemProperty -Path "HKLM:$regPath" -Name 'InstallLocation' -ErrorAction SilentlyContinue
}
$path.InstallLocation
}
function code {
if ($args.Length -eq 0) {
return
}
$path = Get-UninstallPath -ProductId '{771FD6B0-FA20-440A-A002-3B3BAC16DC50}_is1'
$codeCmd = "$path\bin\code.cmd"
# Support wildcards
$codeArgs = @()
$args | ForEach-Object {
if (-not (Test-Path $_ -PathType Leaf)) {
# Add directory names or files that don't exist as-is
$codeArgs += $_
} else {
Get-ChildItem $_ | ForEach-Object {
$codeArgs += $_.FullName # ('"{0}"' -f $_.FullName)
}
}
}
$codeArgs | ForEach-Object {
"Editing $_"
}
& $codeCmd --reuse-window @codeArgs
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment