Link to home
Start Free TrialLog in
Avatar of ARampton
ARamptonFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Adding additional text to each item in a Powershell array

How do I add a constant text string to the end of the text of each item in an array?

I am using Powershell to list the folders in a specific location but need to add a subfolder "\Emails" to each item before I can use it to set the Email folder permissions

$FolderList = dir | Where-Object { $_.PSIsContainer } | ForEach-Object { $_.FullName }
Avatar of Britt Thompson
Britt Thompson
Flag of United States of America image

foreach ($Folder in $FolderList) {
    $FullPath = "$Folder" + "\Emails"
    echo $FullPath
}

Open in new window

Avatar of footech
You can use concatentation (the plus sign), subexpressions, or the format specifier (-f) and variable substitution.  I prefer subexpressions in many cases.  If you wanted to modify the contents of your $FolderList array, you can use the below.
$FolderList = dir | Where-Object { $_.PSIsContainer } |
 ForEach-Object { "$($_.FullName)\Emails" } 

Open in new window

Avatar of ARampton

ASKER

Great renazonse - works just as I wanted - I am just learning some of Powershell

What is the simplest way of now using $FullPath with set-ACL for each folder which now correctly lists in $FullPath?

I used  $NewACL = Get-Acl "S:\SampleFolder\Emails"  where this Emails folder has the permissions I want copied and  Set-Acl "S:\Project XXXX\Emails" $NewACL  but one folder at a time

I would like it to apply Set-Acl to all folders listed in $FullPath
$FolderList = dir | Where-Object { $_.PSIsContainer } | ForEach-Object { $_.FullName }
$NewACL = Get-Acl "S:\SampleFolder\Emails"

foreach ($Folder in $FolderList) {
    $FullPath = "$Folder" + "\Emails"
     Set-Acl "$FullPath" $NewACL
}

Open in new window

Thanks renazonse

Your script achieves just what I need and I understand the sequence except for why the $Folder is in quotes in the line $FullPath = "$Folder" + "\Emails"

How would I amend this script to make the process also include any subfolders below \Emails so making t recursive?
ASKER CERTIFIED SOLUTION
Avatar of Britt Thompson
Britt Thompson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks, this is really useful in applying the same ACLs to all target subfolders

How would this script change when the permissions below the source \Emails vary on each source subfolder and need to set the same on the target subfolders which always mirror the source structure

I think recursive code needs to be adding to the Get-Acl but have never used this before
That's a bit more in depth and would require another question
New Question Raised

Powershell script to copy ACL Permission from source folder structure to target folder structure
From your help I have developed this script to Copy ACL Permissions from Source to Target item by item (included folders and files)

$SFolder = "S:\SourceFolder"
$TFolder = "S:\3060\3078 Test Project 8"     'Targert Folder

$SFolderList = get-childitem -name $SFolder -recurse
foreach ($Folder in $SFolderList) {
          $SFullPath = $SFolder + "\" + "$Folder"
          $TFullPath = $TFolder + "\" + "$Folder"
      $NewACL = Get-ACL "$SFullPath"
Set-ACL "$TFullPath" $NewACL
echo "ACL copied to $TFolder $Folder"
}

Works just how I wanted and is useful in resetting or updating project folder permissions after they have been setup
Really good script which I have used frequently since