1

I am trying to define an array of file paths that I can loop over and apply user permissions to. Some of these paths have spaces in them, and the way I'm trying to define the array variable, I cannot loop over them.

$rootSitePath = "C:\Path"

$paths = $rootSitePath + "\" + "Path1",
         $rootSitePath + "\" + "Path with spaces",
         $rootSitePath = "\" + "Path3"

foreach($path in $paths)
{
   #do stuff
}

Not sure if I need to escape in a certain way??

2 Answers 2

3

No, you don't need to do anything special - but you do need to put parenthesis around the array items as you have them above. try:

$rootSitePath = "C:\Path"

$paths = ($rootSitePath + "\" + "Path1"),
         ($rootSitePath + "\" + "Path with spaces"),
         ($rootSitePath + "\" + "Path3")

foreach($path in $paths)
{
   get-childitem $path
}
Sign up to request clarification or add additional context in comments.

Comments

1

The array , operator has higher precedence than the + operator for concatentation.

So, if you do something like ( simplified example):

$paths = $rootSitePath+"\"+"Path1","path2"

$paths will be a string because it did string concatentation of $rootSitePath\ and Path1 path2 (string representation of the array "Path1", "path2") . So you have to say that the first part before the , is the first element:

$paths = ($rootSitePath+"\"+"Path1"),"path2"

So to solve your problem, enclose each element in parentheses. Apart from that, you are not having problems because your paths had spaces in them.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.