2

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
2
  • Did you try to use quotes? Commented Feb 2, 2015 at 0:27
  • Double quotes. Read up on Powershell quoting. Sometimes, instead of just strings, shells use quoting to ensure no word splitting occurs in arguments and parameters and during dollar substitutions. Commented Feb 2, 2015 at 0:28

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.