As our business continues to focus on providing white labeled Tier 3 IT support services, RMM as a service, and co-managed IT services this blog will be highlighting tips for RMM automation. Here is a script that we came up with to handle a particular client that dumps tons of PDFs into a folder unsorted and wants individual folders created for each unique file name. We have tailored this script to be used not only at that client but for any folder on any Microsoft Windows computer that needs to be sorted in this manner. This script could easily be modified to sort other types of files.
Variables
Here are the variables we are using for this script:
- $SourceFolder = This is the target folder to be sorted
Script Snippet
# Defines the folder that sorted data will go into
$TargetFolder = $SourceFolder + " Sorted"
#Defines how to match files with similar names, in this case we put files with the same name then a dash or underscore and other numbers or letters to be placed in the same folder
$MatchRegEx = "[-_]"
#Grabs only the PDF files in the folder being sorted (modify for other extension types or remove filter to sort all files)
Get-ChildItem -Path $SourceFolder -Filter *.pdf |
ForEach-Object {
#Creates folder name for child folders in sorted data
$FileNameFolder = $_.Name -split $MatchRegEx
$ChildPath = Join-Path -Path $FileNameFolder[0].Replace('.pdf','') -ChildPath $_.Name
[System.IO.FileInfo]$Destination = Join-Path -Path $TargetFolder -ChildPath $ChildPath
#Checks if folder exists and if not creates child folder
if( -not ( Test-Path -Path $Destination.Directory.FullName -erroraction silentlycontinue) ){
New-Item -ItemType Directory -Path $Destination.Directory.FullName
}
#Copies file into child folder
Copy-Item -Path $_.FullName -Destination $Destination.FullName
}
This script is non-destructive meaning that the files are copied and not moved. This script gives screen output of each new child folder created.
If your company is a MSP or wants to become one and automation just seems out of reach, then contact us to run your RMM for you.