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

  • 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.

    Thanks


    #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

  • You say your are quite new to PS, so, it would be prudent for you to really get
    ramped up using all the free resources at your disposal, to limit the confusion
    and frustration you will encounter. 

    See this Q & A with numerous resource listings.

    https://www.reddit.com/r/PowerShell/comments/a1t3zx/free_powershell_learning_resources


    What do this...

    "i have some issue with write-host there are not written quite well"

    ... mean.

    Write-Host will simply output what you give it, wherever you put it.

    Making things look good on the console, is a matter of trial an error and based 100% on personal taste.
    What one considers a good look, is not necessarily a good look for another. So, this is a very opinionated thing.

    Write-Host will take any string and output that like you typed it.
    If you want more control, then you can put text in a formatted string assigned to a variable and use that.


    Example:


    $ScriptMessage = '
    Whatever you want to put here:
        - And more info here
            - Or more here.

    So, you can treat this like a in line text file

    '
    Write-Host $ScriptMessage

    Just remember that write-host, except for those times where you are colorizing screen text, or some specific formatting needs, should be avoided at all costs. Even the founder / creator of PowerShell says...

    Write-Host Considered Harmful | Jeffrey Snover's blog ---
    https://www.jsnover.com/blog/2013/12/07/write-host-considered-harmful

    … again, there are places where it is useful. Yet, in real automation, one is normally not looking at the screen, so writing to the screen is really moot. Also, you should use script logging and or the Start/Stop-Transcript cmdlets if you really need to record and later review script runs. Also, get familiar with the use of the -verbose switch in script / automation for getting inline info for script runs.