简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM