Advanced powershell script ( Copy folder, rename, inherit permissions , modify inside .txt files)

Hello Everyone ,

I`m quite new to Powershell and i need your advanced knowledge for building a powershell script that it will automate the work load in in work enviroment. 

I `ll try to explain as much i can and give you all the details. This script needs to make a copy of folder and content from it , in same path or drive ( C:\ A folder )  which has one txt file in it ( Example.txt )  and rename it under ( C:\ B folder) and inherit all the NTSF permissions from including for .txt file from the source path.

Then after the C:\B folder is copied and all content from it , i want to modify a text line inside .txt file ( in our case in C:\ B folder should include Example.txt)

So for example : ( inside Example.txt file there is this line : Line 1 = Line 2 ) and i want the script to modify the text as  ( Line 1 = Line 3 ).

Many thanks and  i `ll appreciate your help

No Data
  • Hi 

    I ve managed to do something, but i have some issue with write-host there are not written quite well so maybe someone with better knowledge  can polish the script a bit to look nicer, i` ve added also some comments there, like ( file was copied, file was modified ) .

    Much appreciated.


    #Copy current folder and rename the copy folder

    Copy-Item "C:\Test\A folder" "C:\Test\B folder" -recurse -ErrorAction silentlyContinue
    if(-not $?) {write-warning " Failed : File has failed to copy"}
    else {write-host -ForegroundColor Green "Succes : File has been succesfully copied"}

    #Adding permissions to copied folder

    Get-Acl -Path "C:\Test\A folder" | Set-Acl "C:\Test\B folder"

    $savedACL = get-acl "C:\Test\A folder"
    Get-ChildItem "C:\Test\B folder\" -Directory -Recurse | ForEach-Object{
    $_ | Set-Acl -AclObject $savedACL
    if(-not $savedACL) {write-warning " Failed : Permissions not added to copied folder"}
    else {write-host -ForegroundColor Green "Succes : Permissions added to copied folder"}

    #Make changes in txt file

    $file = 'C:\Test\B folder\Example\Example.txt'
    $regex = 'line2'
    (Get-Content $file) -replace $regex, 'line3' | Set-Content $file
    $regex = 'b'
    (Get-Content $file) -replace $regex, 'c' | Set-Content $file

    if(-not $regex) {write-warning " Failed : Changes in config file have NOT been succesufully made"}
    else {write-host -ForegroundColor Green "Succes : Changes in config file have been succesufully made"}

    # Finish PowerShell script