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
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."
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With