I am currently working on a project with powershell to help clean up and save space on our server. I work in a secondary school and we have over 1000 users at our location. I have created a script to create a folder for each user in a location and give only that user and myself access to the folder for them to store their work and general documents on our NAS. The problem I am going to be running into in the future though, is that I don't have a way of archiving their folders yet when the student leaves the school, so in a few years time there is going to be an issue of having only 1000 users, but 2000+ personal folders created, many of which can be archived for a period of time and then deleted to save space on the NAS. The script I have created to generate their folders is below (I have redacted the AD group names and server locations for privacy)
Import-Module ActiveDirectory
Import-Module NTFSSecurity
$ADUsers = Get-ADGroupMember -Identity *user AD Group*
ForEach ($ADUser in $ADUsers)
{
New-Item -ItemType Directory -Path "*Server location*\$($ADUser.sAMAccountname)"
$userfolder = "*Server location*\$($ADUser.sAMAccountname)"
Get-Item $userfolder | Disable-NTFSAccessInheritance
Get-Item $userfolder | Add-NTFSAccess -Account $ADUser.sAMAccountname -AccessRights FullControl
Get-Item $userfolder | Remove-NTFSAccess -account *user AD Group* -AccessRights FullControl
}
This works fine for the folder creation, but I am trying to find a way to archive the user folders of students that have left. I have an idea of creating a CSV file by getting the current usernames from the AD group, then comparing them with the folders in the directory created by the script and have all matching folders stay, but all folders that don't appear in the csv file to be moved to another location for archiving however I am not sure if this is the best way to do it or if I am overlooking a solution that is already in place for this type of thing. Getting a list of users that have left is difficult because they just disappear from the system, I just have a list of current users.
I am currently trying to do this using CSV files, my thinking is to do something like this..
Get-ADGroupMember -Identity *user AD Group* | Select-Object samaccountname | Export-Csv -Path "*server location*\user test csv.csv"
Get-ChildItem "*server location*" | Select-Object PSChildName | Export-Csv -Path "*server location*\folder list.csv"
New-Item -ItemType file *server location*\combined_files.csv –force
Get-Content "*server location*\user test csv.csv", "*server location*\folder list.csv" | Add-Content *server location*\combined_files.csv
The above script creates a CSV file of user's SamAccountNames and a CSV file of folder names that were created by the first script and merges the two CSV files together, creating a new csv file that looks like
a
a
b
c
c
d
But I can't figure out how to remove all entries that are duplicated to leave just the unique entries so the new CSV looks like this
b
d
So that I can use this new CSV file to move the all the folders contained within to the new folder location for archiving.
Is my thinking correct that this is the best way to do this? or is there another better way to skin this cat?
So I have managed to figure out a solution to what I wanted to do and I have posted the script below for anyone else looking for a way to solve the problem. The basic logic is this
I have redacted server locations, adgroups etc but the script will still work once you put your information in there.