≡ Menu

Create a folder/file with today’s date and time using PowerShell

I have seen few people asking for a way to create folder or file with today’s date and time in name using powershell. Though it is easy, many people ask for this. Given the demand, I am authoring this quick post.

Create folder using todays date and time:

$folderName = (Get-Date).tostring("dd-MM-yyyy-hh-mm-ss")            
New-Item -itemType Directory -Path c:\scripts -Name $FolderName

Executing the above code will create a folder with current date and time in c:\scripts folder. You can choose to adjust the format of date time in the above code.

Create file using todays date and time:

$FileName = (Get-Date).tostring("dd-MM-yyyy-hh-mm-ss")            
New-Item -itemType File -Path c:\scripts -Name ($FileName + ".log")

Hope this tips helps you.

Comments on this entry are closed.

  • Mohammed May 15, 2013, 2:20 am

    New-Item -itemType will be ‘File’, not ‘Directory’.

    $FileName = (Get-Date).tostring(“dd-MM-yyyy-hh-mm-ss”)
    New-Item -itemType File -Path c:\scripts -Name ($FileName + “.log”)

    Also convenient format is “yyyyMMdd-hhmmss”. Sorting works better with this format.

    Thanks.

  • dheeraj May 15, 2015, 5:18 pm

    it worked for me , thank you

  • Dhinesh July 15, 2019, 11:55 pm

    Thanks its worked to get file name created with date and time

  • subramanien July 30, 2019, 3:55 am

    I want to create 3 folders,
    1: for current month,
    2: current date
    3: want to save the output Report/ logs into the current date folder

    if the folders are exists should not through any error. Thanks in Advance

    • Wintel Rocks July 31, 2019, 6:52 am

      Please share what you have tried. Happy to help you.

  • Richard June 25, 2021, 8:01 pm

    New-Item -itemType Directory
    should read
    New-Item -itemType File

    • Wintel Rocks July 12, 2021, 3:22 pm

      Thank You Richard. I have corrected it.

  • Anonymous March 8, 2022, 1:59 pm

    How would i write something inside the text file? (like a name or smth)