Command completion script for Powershell

Kopia has built in scripts that can be used to enable parameter completion in bash and zsh but unfortunately not in Powershell. So if you’re on Windows and would like to get auto completion for the command line you can add the following code to your $PROFILE script and it will be available with every new PS session you start:

$scriptblock = {
    param($wordToComplete, $commandAst, $cursorPosition)
	
	$command,$params = $commandAst.ToString() -split " ", 2	
	$params = $params -split " " 
	
	if ( $wordToComplete -ne "" -and $wordToComplete -notlike "-*" ) {
		$params = ($params | select-object -skiplast 1 )
	}	

	& $command --completion-bash $params | Where-Object { $_ -like "$wordToComplete*" } |
	ForEach-Object {
		[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)#
	}
}
Register-ArgumentCompleter -Native -CommandName kopia.exe -ScriptBlock $scriptblock

It will work with commands, sub-commens and flasg (as long as they start with “–”).

Pro Tip: Use ctrl+space to get a list of available command at each point.

3 Likes

Would it be worth integrating this script into the official Scoop package?

1 Like

I’m not sure about scoop. In general, I don’t think it is very commonly used among windows users. That said, I think it would be nice if there was a --completion-script-powershell parameter, like there is for bash and zsh.
I wish I could do it but I’m not a Go developer and in fact, the first time I ever looked at Go code was when I tried to tackle [Feature Request] Make logs in KopiaUI persistent (save to disk rather than store in memory) · Issue #2073 · kopia/kopia · GitHub, which I really need. I didn’t even manage to get “make kopia-ui” to complete the build, so before I can even look at the code I have to figure out how to setup the build environment.

I agree with @ekutner, I don’t use scoop and don’t know many others who do. It is easier to tell people to grab binaries off of github, so it would be better if there was the --completion-script-powershell command.

Hi, it would be great if there is a --completion-script-fish for fish shell as well!