Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate network path + variable

Tags:

powershell

How can I concatenate this path with this variable?

$file = "My file 01.txt" #The file name contains spaces  $readfile = gc "\\server01\folder01\" + ($file) #It doesn't work 

Thanks

like image 529
expirat001 Avatar asked Dec 09 '12 01:12

expirat001


People also ask

How do I concatenate two variables in PowerShell?

In PowerShell, string concatenation is primarily achieved by using the “+” operator. There are also other ways like enclosing the strings inside double quotes, using a join operator, or using the -f operator. $str1="My name is vignesh."

How do I get the PATH environment variable in PowerShell?

Environment variables in PowerShell are stored as PS drive (Env: ). To retrieve all the environment variables stored in the OS you can use the below command. You can also use dir env: command to retrieve all environment variables and values.

How do you assign a path to a variable in PowerShell?

Use $Env:PATH to Set the PATH Environment Variables in Windows PowerShell. Usually, we can set the PATH variable by navigating through the control panel of our operating system. However, inside Windows PowerShell, we can output all our file paths using the $Env:PATH environment variable.


Video Answer


1 Answers

There are a couple of ways. The most simple:

$readfile = gc \\server01\folder01\$file 

Your approach was close:

$readfile = gc ("\\server01\folder01\" + $file) 

You can also use Join-Path e.g.:

$path = Join-Path \\server01\folder01 $file $readfile = gc $path 
like image 171
Keith Hill Avatar answered Sep 29 '22 10:09

Keith Hill