ayankumar1990
ayankumar1990

Reputation: 33

Adding file:// at the beginning of a path using PowerShell

I want to add file:// at the beginning of a path using PowerShell. How can I do that? I tried like this:

$file = "C://Windows"
$path = Join-Path "file://" $file

But is not working.

Upvotes: 1

Views: 104

Answers (2)

RickH
RickH

Reputation: 2486

If you really want the Uri syntax, you can leverage System.Uri like:

[Uri]$tu = New-Object 'Uri'('C:\Windows');
write-host $tu.AbsoluteUri;

That will produce a URI output that looks like:

file:///C:/Windows

(and yes, that third '/' is supposed to be there after the file:. RFC 1738 specifies that there's room for an optional host component in the uri: file://<host>/<path>)

Upvotes: 4

Raf
Raf

Reputation: 10117

$path = "file://" + $file

Also you don' need to escape forward slash in Powershell so $file can be $file="C:/Windows"

Upvotes: 1

Related Questions