top | item 39187567

(no title)

mackrevinack | 2 years ago

i only recently noticed that powershell accepts ~ as a shortcut for your user folder. anyone know how long that had been a thing?

discuss

order

sedatk|2 years ago

Since 1.0, but it’s PowerShell specific.

jasomill|2 years ago

PowerShell's ~ can be counterintuitive, as it's relative to the current location's path and defined by a property of the current location's PSProvider:

  PS C:\> Get-PSDrive C,S,HKLM | select Name,{$_.Provider.Name},{$_.Provider.Home}
  
  Name $_.Provider.Name $_.Provider.Home
  ---- ---------------- ----------------
  C    FileSystem       C:\Users\jtm
  S    FileSystem       C:\Users\jtm
  HKLM Registry
  
  PS C:\Users\jtm> Get-PSDrive C,S,HKCU,HKLM | select Name,{$_.Provider.Name},{$_.Provider.Home}
  
  Name $_.Provider.Name $_.Provider.Home
  ---- ---------------- ----------------
  C    FileSystem       C:\Users\jtm
  S    FileSystem       C:\Users\jtm
  HKCU Registry
  HKLM Registry
  
  PS C:\Users\jtm> cd HKLM:
  PS HKLM:\> cd ~
  Set-Location: Home location for this provider is not set. To set the home location, call "(get-psprovider 'Registry').Home = 'path'".
  PS HKLM:\> (Get-Location).Provider.Home = 'C:\Program Files\Microsoft Office'
  PS HKLM:\> cd ~
  PS C:\Program Files\Microsoft Office> (Get-Location).Provider.Home = 'HKLM:\SYSTEM\CurrentControlSet'
  PS C:\Program Files\Microsoft Office> cd S:
  PS S:\> cd ~
  PS HKLM:\SYSTEM\CurrentControlSet> (Get-Location).Provider.Home = '..'
  PS HKLM:\SYSTEM\CurrentControlSet> cd ~
  PS HKLM:\SYSTEM> cd ~
  PS HKLM:\> Get-PSDrive C,S,HKCU,HKLM | select Name,{$_.Provider.Name},{$_.Provider.Home}
  
  Name $_.Provider.Name $_.Provider.Home
  ---- ---------------- ----------------
  C    FileSystem       HKLM:\SYSTEM\CurrentControlSet
  S    FileSystem       HKLM:\SYSTEM\CurrentControlSet
  HKCU Registry         ..
  HKLM Registry         ..
  
  PS HKLM:\>

HeckFeck|2 years ago

In cmd you still have the very intuitive %USERPROFILE%. It can be shortened by a custom environment variable if you own the box.

chungy|2 years ago

Even in the Unix world, it's shell-specific, but most shells tend to expand ~ out to $HOME (which in turn gets expanded to the actual path). They also have complicated rules about when ~ characters are expanded or not.