Author Posts

November 3, 2015 at 5:19 pm

Im trying to create a new folder in a blank directory and have the script make a folder for each month for the last 3 years. Such as 2013 (Jan, Feb, Mar....), 2014 (Jan, Feb, Mar....) and so on. I know how to make just normal folders. But im wondering an easy way to use the Get-Date function to make these folders. Is there any easy way to do this?

November 3, 2015 at 6:46 pm

Good Question. Here is my take at this.

2012..2015 | %{New-Item -Path C:\temp\ -ItemType Directory -Name $_}

for ($i = 2;$i -le 5;$i++){
1..12 | % {New-Item -Path "C:\temp\201$i" -ItemType Directory -Name (Get-Date "1/$_/2015" -Format M).TrimStart('01 ')}

}

I'm sure someone may have a more elegant way of doing this 🙂

November 3, 2015 at 8:49 pm

It's a little unclear from your description what you ultimately want the folder path to look like. I am assuming c:\temp\2013\Mar. The following uses the US culture for the get-date input, but you can change it to whatever culture you use.

# define starting point
$root = "C:\ephemeral\"

#create the annual folders
2012..2015 | foreach {
    $yr = (New-Item -Path $root -ItemType Directory -Name $_).Name
    # create monthly folders
    1..12 | foreach {
        New-Item -Path "$root\$yr" -ItemType Directory -Name $(Get-Date "$_/1/$yr" -Format MMM)
    }
}

November 4, 2015 at 9:54 am

That is more or less what I was looking for! I appreciate the help. Just going to take that and tweak it a bit to how I need it. Thanks guys!