Reputation: 4990
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??
Upvotes: 1
Views: 8195
Reputation: 301147
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.
Upvotes: 1
Reputation: 683
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
}
Upvotes: 3