?
Solved

Adding additional text to each item in a Powershell array

Posted on 2013-12-09
11
Medium Priority
?
2,253 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
Ransomware Attacks Keeping You Up at Night?

Will your organization be ransomware's next victim?  The good news is that these attacks are predicable and therefore preventable. Learn more about how you can  stop a ransomware attacks before encryption takes place with our Ransomware Prevention Kit!

 
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 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

765 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