Steve Crane
Steve Crane

Reputation: 4440

How to pass an unqualified file name to a Powershell script?

I got some Powershell code from the answer to a different question and created this script, modifying it slightly so I could pass in the file name.

$file = [System.io.File]::Open($Args[0], 'Open', 'Read', 'None')
Write-Host "Press any key to continue..."
$null = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
$file.Close()

It works fine if I run it (from a Powershell window) with an unqualified file when in my home directory or with a fully qualified file name in any directory. The script is lockfile.ps1 in a utilities folder on my path and these calls all work as expected.

C:\Users\SteveC.AFRICA> . lockfile abc.txt
C:\Users\SteveC.AFRICA> lockfile abc.txt
C:\Users\SteveC.AFRICA> lockfile .\abc.txt
C:\Users\SteveC.AFRICA> lockfile D:\Junk\abc.txt

However, when the current directory is not my home directory, I need to fully qualify the file name even for files in the current directory. So this call works

D:\Junk> lockfile D:\Junk\abc.txt

but these do not

D:\Junk> lockfile abc.txt
D:\Junk> lockfile .\abc.txt

both reporting the following error, indicating they are expecting to find the unqualified file named in my home directory.

Exception calling "Open" with "4" argument(s): "Could not find file 'C:\Users\SteveC.AFRICA\abc.txt'."

Why is this, and what do I need to do to allow scripts like this to correctly accept unqualified file names as parameters?

Upvotes: 0

Views: 414

Answers (1)

user4003407
user4003407

Reputation: 22132

I think, that you mixing two things: PowerShell path and provider (FileSystem provider, especially) path. This two things are different.

In PowerShell you can create drives as you pleased, for example:

New-PSDrive -Name Win -PSProvider FileSystem -Root C:\Windows

After that you can write like this:

Get-Content Win:\System.ini

Of course, if you write something like this:

[System.IO.File]::ReadAllLines('Win:\System.ini')

it will fail, due to ReadAllLines does not know anything about PowerShell and its Win drive. You need to convert PowerShell path to FileSystem provider path before to passing it to file system APIs. It can be done with Convert-Path cmdlet:

Convert-Path Win:\System.ini

To make it easy to mix this things simplify transition to PowerShell, FileSystem provider automatically maps file system drives to PowerShell drives with same name. So, you normally do not notice distinction. But for relative paths it is not work that well, because PowerShell current location and process current working directory are different things as well.

Upvotes: 1

Related Questions