Jaydeep Chaudhari
Jaydeep Chaudhari

Reputation: 430

How ignore spaces and special characters in folder names

We have storage shared as \\192.168.1.26\e$. There are several folders (all shared) which are used by different departments. I wanted to get the owners and users who has access to folders, which are in those share folders.

I have created this PowerShell script for that:

$path = "\\192.168.1.26\e$\"
$getlist = Get-ChildItem -Path \\192.1681.26\e$ | Select-Object Name
foreach ($folder in $getlist) {
    $out = '\\192.168.1.26\e$\{0}' -f $folder.name
    #write-host $out
    #prep arguments
    $getargs = $out,'-ad | where {$_.psiscontainer -eq $true} | get-acl | Select-Object path,owner,accesstostring | fl'
    write-host $getargs
    gci $getargs
}

From $getlist = Get-ChildItem -Path \\192.1681.26\e$ | Select-Object Name I get all folders listed. However some folder names contains spaces and special characters, which causes the rest of the script to fail.

Examples:

It-&-Network
MIS
Account
Sales Reports
Market Report
DATA

When I run the below command it works as expected

gci \\192.168.1.26\e$\MIS | where {$_.psiscontainer -eq $true} | get-acl | Select-Object path,owner,group,accesstostring | fl

But I get an error on this command:

gci \\192.168.1.26\e$\Sales Reports | where {$_.psiscontainer -eq $true} | get-acl | Select-Object path,owner,group,accesstostring | fl

Is there any way to fix this?

Upvotes: 0

Views: 1703

Answers (1)

Mikhail Aksenov
Mikhail Aksenov

Reputation: 934

Solution from Add double quotes to variable to escape space

$path = '\\localhost\f$\tmp'
$getlist = Get-ChildItem -Path $path | Select-Object Name
foreach ($folder in $getlist) {
    $out = '"{0}\{1}"' -f $path,$folder.name 
    $getargs = $out
    write-host $getargs
    $extraparam = '-ad | where {$_.psiscontainer -eq $true} | get-acl | Select-Object path,owner,accesstostring | fl'
    $run_command = 'gci {0} {1}' -f $getargs,$extraparam
    write-host $run_command
    Invoke-Expression $run_command
}

Also it can helps you - https://ss64.com/ps/syntax-esc.html Updated for local disk example, working on my machine.

Upvotes: 1

Related Questions