
The Code
On Windows, if you open up PowerShell ISE, you can paste this into a new script file.
It will run from the folder it is placed in so make sure you have a blank folder to start.
Create a new text file in the same directory called “People.txt”. If you have another use case then rename this to whatever is appropriate but make sure you rename the file name in the script below.
How it’s used
This references the “People.txt” file and makes a folder for every line entered into the file. It then copies that file into a secondary file to reference from for future runs.
When run a second time, it compares the “OldNames.txt” it created to the “People.txt” that may be updated with additional names, and archives names that are no longer applicable if they were removed from the “People” list. Additional people added will have a new folder created.
This is for roles that manage a lot of assets, or needs to keep records for individuals in your organization, or if you just need to make a folder managing tool from a text file for any home use.
#Sets the path this script is running from
$Path = $PSScriptRoot
#Gathers the names from the "Old" file
$OldNames = Get-Content -Path $Path\OldNames.txt
#Gathers the names from the "People" File.
$People = Get-Content -Path $Path\People.txt
# Archive the folders of people that have left
ForEach ($oldPerson in $OldNames) {
if ($oldPerson -notin $People) {
Move-Item -Path $Path\$oldPerson -Destination "$Path\1. Archive" -Force
}
}
# Make Folders for new people
ForEach ($Person in $People) {
# Create a new folder for each line.
New-Item -ItemType Directory -Path $Path -Name $Person -Force
}
#Archive OldNames and Rename People. Make new blank People file
Move-Item -Path $Path\OldNames.txt -Destination "$Path\1. Archive\1. OldNames" -Force
Copy-Item -Path $Path\People.txt -Destination "$Path\OldNames.txt" -Force
#New-Item -ItemType File -Name People.txt -Force
I’m not an expert in code, or PowerShell, but if you have any ways this could be run better please let me know! I needed it for myself but figured I would share it if it’s useful.
Leave a Reply