Dear All,
I am trying to create a script in order to clean-up UNC path directories. I am working with hundreds of shares where the folder structure is identical so I would like to create a script to move older than 90 days items, to a backup folder to the root of the individual shares. My only problem that I can’t exclude specific directories which content should not be moved ever (audit,reminder).
Here is my script so far:
$SourceDir = "\SERVER\SHARE$\export\APP%FOLDER%" ### The %FOLDER% is not in the actual script, just put there to indicate.
$DestinationDir = "\SERVER\SHARE$\export_BACKUP!\APP"
$files = get-childitem $SourceDir . -Recurse
Foreach ($file in $files)
{
$Directory = $DestinationDir + “” + $file.CreationTime.Date.ToString(‘MM-yyyy’)
if (!(Test-Path $Directory))
{
New-Item $directory -type directory
}
If($file.LastWriteTime -lt (Get-Date).adddays(-90).date)
{
Move-Item $file.fullname $Directory -Exclude \SERVER\SHARE$\export\APP[]\audit*.“,\SERVER\SHARE$\export\APP[]\reminder*.” -WhatIf
}
}
I have one issue and one enhancement request.
The issue that it does not matters if i put the -Exclude after the " $files = get-childitem $SourceDir . -Recurse " part or after Move-Item it does not works and moving the audit, etc. folders as well. I get that the issue is the part in the $SourceDir - %FOLDERS%. In every UNC path within the APP folder there are hundreds of subfolders, which i would like to wildcard, so exclude all “audit” folder within %FOLDERS%, and move everything else which is older than 90 days.
To summ here is a tree view:
APP
├───folder1
│ ├───audit
│ │ ├───folders
│ │ ├───folders
│ │ ├───folders
├───folder2
│ ├───audit
│ │ ├───folders
│ │ ├───folders
│ │ ├───folders
├───folder3
│ ├───audit
│ │ ├───folders
│ │ ├───folders
│ │ ├───folders
So everything within the audit folders (and some else) should stay and everything not there and older than 90 days should moved to a backup folder.
And here is the enhancement request. I tried to do it to create the exact same architecture as you see above within the _BACKUP!\APP folder, but I could not figure out how to. Currently it is creating folders based on Month and Year and moves everything there based on item creation date, however it would be really nice if I could maintain the same folder structure within the backup as well. I tried to pass the below command to “New-Item -Directory”, but it is not really working out, creating the folder named of all folders with APP dir, not individual ones, and also does not know how to match up source and destination so make sure that Folder1 content will got to _BACKUP!\APP\Folder1\DateandTime\
$Files = Get-ChildItem $SourceDir . -Directory |
ForEach-Object
{
$Property = “\10.31.1.50\marriott2_g_opera$\export\OPERA$($_.Name)”
######## MISSING CODE ############
}
Tried to filter like this as well, which works, however cannot create directories by every entry, just one with all entry name:
$Property = Get-ChildItem "\SERVER\SHARE$\export\APP*" | Where-Object {$.PSIsContainer} | Foreach-Object {$.Name}
Off course this is just an enhancement which would be nice to have, but the main priority is to make the -Exclude parameter working.
Thanks for the Community help in advance.