Solved

Adding additional text to each item in a Powershell array

Posted on 2013-12-09
11
2,090 Views
Last Modified: 2013-12-19
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 }
0
Comment
Question by:ARampton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
11 Comments
 
LVL 30

Expert Comment

by:Britt Thompson
ID: 39706470
foreach ($Folder in $FolderList) {
    $FullPath = "$Folder" + "\Emails"
    echo $FullPath
}

Open in new window

0
 
LVL 40

Expert Comment

by:footech
ID: 39706504
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

0
 

Author Comment

by:ARampton
ID: 39707073
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
0
Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

 
LVL 30

Expert Comment

by:Britt Thompson
ID: 39707264
$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

0
 

Author Comment

by:ARampton
ID: 39713772
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?
0
 
LVL 30

Accepted Solution

by:
Britt Thompson earned 500 total points
ID: 39713800
$FolderList = dir | Where-Object { $_.PSIsContainer } | ForEach-Object { $_.FullName }
$NewACL = Get-Acl "S:\SampleFolder\Emails"
foreach ($Folder in $FolderList) {
    $FullPath = "$Folder" + "\Emails"
    Get-ChildItem $FullPath -recurse -Force |%{
   $NewACL | Set-Acl -Path $_.fullname
}
}

The quotes are they in case there's spaces in the path. Although you may not need it there.
0
 

Author Comment

by:ARampton
ID: 39713850
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
0
 
LVL 30

Expert Comment

by:Britt Thompson
ID: 39713950
That's a bit more in depth and would require another question
0
 

Author Comment

by:ARampton
ID: 39714710
New Question Raised

Powershell script to copy ACL Permission from source folder structure to target folder structure
0
 

Author Comment

by:ARampton
ID: 39728920
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
0
 

Author Closing Comment

by:ARampton
ID: 39728922
Really good script which I have used frequently since
0

Featured Post

Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question