Create Folder

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Slade Benda Slade Benda 11 months, 3 weeks ago.

  • Author
  • #31641
    Profile photo of Slade Benda
    Slade Benda

    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?

  • #31642
    Profile photo of Flynn Bundy
    Flynn Bundy

    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 🙂

  • #31643
    Profile photo of Bob McCoy
    Bob McCoy

    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)
  • #31673
    Profile photo of Slade Benda
    Slade Benda

    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!

You must be logged in to reply to this topic.