none
PowerShell to Copy folders with read only attributes RRS feed

  • Question

  • Hi,
    I’m new to PowerShell and need guidance on a script I’m currently building. 
    Requirements - the script needs to look through a bunch of files to search for a specific date. If the file is older than this date it needs to be moved to another location with read only attributes.  With regards to the read only attributes this means the share permissions are not changed just the NTFS permissions are altered. This is so the file is unable to be altered. If the file needs updating, then it is moved to a new location and saved.

    This is what I have so far. It sort of works but when a file is copied no folder structure is added.  
    $StrSource ="D:\deptkp\*.*"  #path to source files
    $StrTarget= "D:\archive"     #path to where source files are going
    $Date = "3"
    
    Get-ChildItem -Recurse -Path $StrSource | Where-Object {$_.LastWriteTime -lt (get-date).AddDays($Date)} | Copy-Item -Destination $StrTarget -Force | Set-ItemProperty -Name IsReadOnly -Value $true 

    Also is there a way to output the copy progress to the screen

    Currently being run a server 2012r2

    Thanks Nick 



    Nicholas Refalo


    • Edited by AUS IT Worker Monday, November 27, 2017 1:52 AM
    • Moved by Bill_Stewart Friday, January 26, 2018 2:43 PM This is not "fix/debug/rewrite my script for me" forum
    Monday, November 27, 2017 1:51 AM

All replies

  • First learn PowerShell.

    Second do not place all code on one line - it is unreadable and unmanageable.

    To output results use "PassThru" argument where available.


    \_(ツ)_/

    Monday, November 27, 2017 1:58 AM
  • And try not to complicate things with unnecessary variables:

    Get-ChildItem D:\deptkp\* -Recurse | 
        Where-Object { $_.LastWriteTime -lt (get-date).AddDays(-3) } |
        Copy-Item -Destination D:\archive -Force -PassThru


    \_(ツ)_/

    Monday, November 27, 2017 2:02 AM
  • Thank you jrv for your reply, yes I like it simple. I will test the changes you purposed and post back thanks 

    Nicholas Refalo

    Monday, November 27, 2017 4:13 AM