A friend of mine found this enormously useful, one of the things PowerShell can do easily that is (apparently) helpful for people who want nothing to do with it otherwise (where it may be either PowerShell or Windows).

PuTTY stores session settings in the Registry, this little snippet using the PowerShell Registry Provider to update PuTTY session values. Anything goes as long as you can figure out the registry value you want to change.

Everything is in short-hand to make it, well, short. So, for reference this is what the shorthand really means:

  • ls is Get-ChildItem
  • sp is Set-ItemProperty
  • % is ForEach-Object (a loop operator)

List all sessions

ls HKCU:\Software\SimonTatham\PuTTY\Sessions

List some sessions

ls HKCU:\Software\SimonTatham\PuTTY\Sessions\*.domain.example
ls HKCU:\Software\SimonTatham\PuTTY\Sessions\*-rtr-*
ls HKCU:\Software\SimonTatham\PuTTY\Sessions\*-sw-*

More complex filtering can be performed using Where-Object (?), but that breaks the simplicity I’m striving for here.

The items returned by ls (Get-ChildItem) can be piped directly into sp (Set-ItemProperty) to change things.

Set the console size

ls HKCU:\Software\SimonTatham\PuTTY\Sessions | sp -name TermWidth -value 140
ls HKCU:\Software\SimonTatham\PuTTY\Sessions | sp -name TermHeight -value 50

Set a proxy

ls HKCU:\Software\SimonTatham\PuTTY\Sessions | sp -name ProxyHost -value "proxy.domain.example"
ls HKCU:\Software\SimonTatham\PuTTY\Sessions | sp -name ProxyPort -value 3128

Add a username

This assumes none of the sessions have a username yet, if it were to run against a session with a username it would double up. That is, me@host becomes me@me@host.

ls HKCU:\Software\SimonTatham\PuTTY\Sessions | %{ sp $_.PSPath -name HostName -value "me@$($_.GetValue('hostname'))" }

Replace a username

Swapping out root@ with me@, a regular expression matches ^root at the beginning of the HostName. If the session does not use root nothing will change.

ls HKCU:\Software\SimonTatham\PuTTY\Sessions | %{ sp $_.PSPath -name HostName -value ($_.GetValue('hostname') -replace '^root@', 'me@') }