@@ -24,7 +24,7 @@ var ErrInstallFailed = errors.New("failed to install one or more profiles")
2424var validShells = shell .SupportedShells ()
2525
2626// Skip sh when installing all shells as it generally does not have a profile defined
27- var defaultShells = []string {shell .ShellBash , shell .ShellFish , shell .ShellPowershell , shell .ShellZsh }
27+ var defaultShells = []string {shell .ShellBash , shell .ShellFish , shell .ShellPowershell , shell .ShellPwsh , shell . ShellZsh }
2828
2929type CmdInstall struct {
3030 * subcommand.SubCommand
@@ -85,7 +85,8 @@ func (n *CmdInstall) RunE(cmd *cobra.Command, args []string) error {
8585 shell .ShellBash : {Name : "bash" , Binary : "bash" },
8686 shell .ShellZsh : {Name : "zsh" , Binary : "zsh" },
8787 shell .ShellPosixShell : {Name : "sh" , Binary : "sh" },
88- shell .ShellPowershell : {Name : "powershell" , Binary : "pwsh" },
88+ shell .ShellPwsh : {Name : "pwsh" , Binary : "pwsh" },
89+ shell .ShellPowershell : {Name : "powershell" , Binary : "powershell" },
8990 shell .ShellFish : {Name : "fish" , Binary : "fish" },
9091 }
9192
@@ -151,6 +152,17 @@ func (n *CmdInstall) RunE(cmd *cobra.Command, args []string) error {
151152 return summaryErr
152153}
153154
155+ func addExecutableBinaryToPowerShellPath (existingSnippet string ) (string , error ) {
156+ exePath , err := os .Executable ()
157+ if err != nil {
158+ return existingSnippet , err
159+ }
160+ exeDir := filepath .Dir (exePath )
161+
162+ snippet := fmt .Sprintf ("$env:PATH += '%c%s'; " , filepath .ListSeparator , exeDir ) + existingSnippet
163+ return snippet , nil
164+ }
165+
154166func (n * CmdInstall ) InstallProfile (shellType string ) (bool , error ) {
155167 changed := false
156168 cfg , err := n .factory .Config ()
@@ -188,8 +200,19 @@ func (n *CmdInstall) InstallProfile(shellType string) (bool, error) {
188200 profilePath = "~/.config/fish/config.fish"
189201 profileSnippet = "c8y cli profile --shell fish | source"
190202 case shell .ShellPowershell :
203+ profilePath = "~/Documents/WindowsPowerShell/Microsoft.PowerShell_profile.ps1"
204+ if snippet , err := addExecutableBinaryToPowerShellPath ("c8y cli profile --shell powershell | Out-String | Invoke-Expression" ); err == nil {
205+ profileSnippet = snippet
206+ } else {
207+ cfg .Logger .Warnf ("Failed to detect binary path. %s" , err )
208+ }
209+ case shell .ShellPwsh :
191210 profilePath = "~/.config/powershell/Microsoft.PowerShell_profile.ps1"
192- profileSnippet = "c8y cli profile --shell powershell | Out-String | Invoke-Expression"
211+ if snippet , err := addExecutableBinaryToPowerShellPath ("c8y cli profile --shell powershell | Out-String | Invoke-Expression" ); err == nil {
212+ profileSnippet = snippet
213+ } else {
214+ cfg .Logger .Warnf ("Failed to detect binary path. %s" , err )
215+ }
193216 }
194217
195218 if profilePath == "" {
0 commit comments