≡ 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 Directory -Path c:\scripts -Name ($FileName + ".log")

Hope this tips helps you.

{ 2 comments… add one }
  • 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

Leave a Comment