#PSTip What’s the difference between ${c:\test.txt}, ${c:test.txt}, and ${test.txt}

Each PowerShell drive (PSDrive) has its own notion of current working directory (CurrentLocation):

PS C:\> Get-PSDrive d,c

Name Used (GB) Free (GB) Provider   Root CurrentLocation
---- --------- --------- --------   ---- ---------------
D       295.59    102.49 FileSystem D:\
C       277.61    205.11 FileSystem C:\             temp

In the variable notation, if the path after the drive specifier is a relative path, then it will be resolved relative to the current working directory for the specified drive. If the drive is not specified, then the current directory for the current drive is used.

  • ${c:\test.txt} refers to the file in the root of the C: drive
  • ${c:test.txt} refers to the file in the current working directory on the C: drive
  • ${test.txt} refers to the file in the current working directory on the current drive

This works for commands as well:

# look up test.txt file in the root of the C: drive
PS C:\temp> Get-Content C:\test.txt

# look up test.txt file relative to the current directory for the C: drive
# in this case that's C:\temp\test.txt
PS C:\temp> Get-Content C:test.txt

# look up test.txt relative to the current directory on the current drive
PS C:\temp> cd D:
PS D:\>
# in this case that's D:\test.txt
PS D:\> Get-Content test.txt

About the author: Aleksandar Nikolic

Aleksandar is a PowerShell MVP and a co-founder of PowerShell Magazine. He is a frequent speaker at the conferences and participates regularly in IT Pro user groups worldwide. Recently, Aleksandar co-authored the highly respected Administrator’s Guide to Windows PowerShell Remoting. Follow him on Twitter at @alexandair

Related Posts

%d bloggers like this: