Monthly Archive

Categories

PowerShell v6.1 working directory

PowerShell v6.1 introduced a very annoying change in that a –workingdirectory parameter was added to pwsh.exe. When you install v6.1 working directory is automatically set to ~ which is your home folder. The workingdirectory doesn’t play nice with your profile. In my profile I set the location to C:\scripts where I keep my work in progress and scripts I use on a frequent basis. If –workingdirectory is set to ~ my profile is ignored depending on whether I’m running as administrator (profile is honoured) or not. This is how I got round the annoyance of the PowerShell v6.1 working directory.

 

This is for WINDOWS only. If you’re on Linux or mac you’ll need to adapt this as appropriate.

 

First option is to go in and edit the icon – Right click the PowerShell icon on start menu. Select More –> Open file location. Select the PowerShell shortcut, right click and select Properties. Edit the working directory parameter. Either remove is and the folder or change the folder.

 

So that’s manual and painful. Unfortunately, there isn’t any functionality in PowerShell to manage short cuts. There is a way using COM objects.

function get-icon {

$wsshell = New-Object -ComObject 'WScript.Shell'

$icon = $wsshell.CreateShortcut('C:\ProgramData\Microsoft\Windows\Start Menu\Programs\PowerShell\PowerShell 6 (x64).lnk')

$icon

}

 

Create an instance of the WScript.Shell COM object. Notice you have to use the CreateShortcut method to get the shortcut properties!

 

The important line in the output is

Arguments : -WorkingDirectory C:\Scripts

 

You can clear the arguments

function clear-argument {

$wsshell = New-Object -ComObject 'WScript.Shell'

$icon = $wsshell.CreateShortcut('C:\ProgramData\Microsoft\Windows\Start Menu\Programs\PowerShell\PowerShell 6 (x64).lnk')

$icon.Arguments = ''

$icon.Save()

}

 

Ideally, I suppose you should just remove the –workingdirectory argument but quick and dirty works for me on this.

 

If you don’t have a –workingdirectory parameter set your profile isn’t honoured with regard to setting locations unless you run as administrator.

To set the working directory

function set-argument {

$wsshell = New-Object -ComObject 'WScript.Shell'

$icon = $wsshell.CreateShortcut('C:\ProgramData\Microsoft\Windows\Start Menu\Programs\PowerShell\PowerShell 6 (x64).lnk')

$icon.Arguments = '-WorkingDirectory C:\Scripts'

$icon.Save()

}

 

If you change the argument in this manner you may need to recreate any icons you pinned to the start bar.

Comments are closed.