Jump to content

Powershell script to create folders within folders...?


Recommended Posts

Posted

Hi,

 

It's 'Performance Management' time again and I've been asked to create a new folder (2022 2023) for all staff within their own folder in the 'Performance Management' folder. Seems like it should be easy by I'm struggling....

 

Structure of the folder is as follows:

 

F:\Staff\Performance Management Staff Folders\Teacher 1\

 

Currently they have a 2019 2020 folder and a 2021 2022 folder, so I need to create a 2022 2023 folder in each 'Teacher' folder.

 

I had a script that originally created all the staff folders from a .csv that I thought I could modify but...

 

So far the first line is this (which I have found to get the folder names and create a .csv - originally I just had a .csv with staff names) :

 

# Create list of folders

Get-ChildItem 'F:\Staff\Performance Management Staff Folders' | Select-Object name | Export-Csv -path C:\Data\folders.csv -NoTypeInfo

 

Which is OK, but also returns the name of a few files as well as the folders, I can manually edit the .csv and remove them but to automate the whole thing I need to exclude those or find a 'Select-Object' that only pulls folder names.

 

The next part creates the folders, but I don't know how I get a 2022 2023 folder in there (as it originally just created a folder with the staff names from the .csv).

 

 

# location where the subfolders will be created
$folder = "E:\Staff$\Performance Management Staff Folders\"

# csv file with folder names
# csv layout as follows
# 
# name
# STAFF 1
# STAFF 2
# STAFF 3

$name = Import-Csv C:\data\folders.csv

Foreach ($line in $name){

[b]New-Item -path $folder -Name $line.Name -Type Directory[/b]
}

 

So it is probably the last line where it should kind of read 'for each folder in the .csv create a folder 2022 2023'...?

Posted

Something like this at the bottom of the loop to go into the folder, make a new directory and then go back up a level?

 

Set-Location -Path $line.name
New-Item 2022-2023 -Type Directory
Set-location -Path ..

 

Bonus points to set the permissions with the NTFS-Security module. as long as the username is in the speadsheet

 

Import-Module NTFSSecurity

..

Add-NTFSAccess -Path $FullPath -Account "$Username" -AccessRights Modify -AccessType Allow

  • Thanks 1
Posted

Funny isn't it... I ended up doing them manually... It took 10 minutes as opposed to the hour or two I spent researching how to do it in Powershell... and all the random folders I ended up creating all over the place...

 

For small jobs (which is all I ever really do) learning Powershell has always seemed like overkill (and I know I suffer from the fact that in such a small environment it is nearly always quicker to do something manually than it is to work out how to automate it).

 

Then someone drops a 1 line CMD (to my credit I had tried something similar but couldn't get it to work for some reason)... which puts me off Powershell even more and reinforces my imposter syndrome no end! :p

 

I did a quick test:

 

FOR /d %A IN ("C:\Users\XXXXX\Desktop\New Folder\*") DO mkdir "%A\2022 2023"

 

Worked perfectly!

  • Thanks 1
Posted (edited)

Sometimes I even use Excel..

 

A quick dir /b /ad >list.txt to get folder names.

 

Open the list in Excel and then use concatenate to construct some appropriate mkdir statements and then paste them back into a command prompt.

Edited by andy_b
Posted (edited)

So this is how I would go about it along with a few thoughts.

gci -directory e:\staff\ | %{set-location "$($_.FullName)\Performance Management Staff Folders"; New-Item .\2022_23 -Item-Type Directory}

 

That about covers the code.. but leads me on to spending time on learning. Now I wrote the above line of code in a few mins and that's because I have done the hard work and learned how to use PS equally I could have written the command line statement as well (although these days that would have took me longer as its now not what I do these days!) learning how to use PS is an investment and yes it can be a frustrating one sometimes and as it turns out just like everything else there are multiple ways in PS (hell all of IT) to skin the same cat, and there is nothing wrong with leverage some old school command line stuff to boot.

 

Choosing not to learn PS is in my mind not the right call as I will tell you now that you can not call the graph API to get you all the sign in details for a user from Azure AD, I know that this is a polar opposite but my point is about the journey and you will never start on that journey without taking the first step. Much like I did not learn how to use the command prompt in MS DOS all those years ago without spending a whole lot of time getting to know the basics. This is also true throughout my life in other areas, I did not build my first brick wall without spending a chunk of time not building a brick wall! Nor did I plaster my fist wall without the hills and valleys of my fist 20 attempts and I did not learn how to fix cars without a lot of time spent in books or learning from others around, the list goes on... But the story is the same anything worth doing takes effort but the rewards and the feeling of satisfaction once you get there is well worth it.

Edited by HPlum78
Posted

Just spotted an error in my line of code! This is what happens when you use a phone and not VS Code or the ISE to write your code! (Also tells you that I ain't tested the code..)

 

gci -directory e:\staff\ | %{set-location "$($_.FullName)\Performance Management Staff Folders"; New-Item .\2022_23 -ItemType Directory}

  • Thanks 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...