CS3000911
CS3000911

Reputation: 131

How to handle spaces in file path

I am having trouble with the following script. It seems to freakout with the spaces in the file path that I am checking for. Any ideas on how to not have the PoweShell freak out, with the Program Files (x86)?

GC C:\server.txt | %{

    $server = $_
    if (Test-Path \\$server\c$\Program Files (x86)\some_dir\test.txt){
            New-Object PSOBject -Property @{
            Server = $server
            Status = "Yes"
            }
        }else {
        New-Object PSOBject -Property @{
        Server = $server
        Status = "No"
        }
    }
}| Export-Csv C:\temp\report.csv -nti

Upvotes: 2

Views: 14689

Answers (1)

user2555451
user2555451

Reputation:

If your file path contains whitespace, you will need to use a string literal:

if (Test-Path "\\$server\c$\Program Files (x86)\some_dir\test.txt"){

Make sure you use double quotes though so that variables are properly expanded.

Upvotes: 4

Related Questions