I’m creating a cmdlet for PowerShell and I need to be able to call Set-Location (aka cd) from within a the cmdlet. I would do it like this
var setLocation = new Microsoft.PowerShell.Commands.SetLocationCommand();
setLocation.Path = path;
setLocation.Invoke();
except that it gives me an error that says You cannot invoke a cmdlet that derrives from PSCmdlet. I’d like to use Set-Location, but I’d be happy with just simply changing the directory of the shell.
The best answer I could find was to use
InvokeScriptto change directory:It’s possible that there’s a “more C#” way to do this, but I couldn’t find it.