Create new folder with current date and time stamp

We can create new folders or files  with current time stamp as folder\file name using PowerShell which can be included into script.

Example 1: Creating file with current time & date

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

Example 2: Creating folder with current time & date

$date= ((Get-Date).ToString('dd MMM yyyy hh_mm_ss' ))
New-Item -ItemType Directory -Path "D:\Powershell\folder 1\$date"
Create folder with time stamp using powershell
Create folder with time stamp using Powershell

Example 3 : Other formats :

#Other format1 - Date as folder name & time as subfolder name
$date= ((Get-Date).ToString('dd MMM yyyy' ))
$time= ((Get-Date).ToString('hh mm ss' ))
New-Item -ItemType Directory -Path "D:\Powershell\folder 1\$date\$time"

#Another method2 - using "Name" parameter
$date= ((Get-Date).ToString('dd MM yy hh_mm_ss' )) New-Item -ItemType Directory -Path "D:\Powershell\folder 1\sun " -name $date

#Another method - using "md" command
$date= ((Get-Date).ToString('dd MM yy hh_mm_ss' ))
md "D:\Powershell\folder\sun\$date"

Example 4 : To create file & folder with timestamp :

#Date as folder and time as file
$date = ((Get-Date).tostring("dd-MM-yyyy")) 
$time = ((Get-Date).tostring("hh-mm-ss"))
New-Item -itemType Directory -Path D:\powershell\ -Name $date 
New-Item -itemType File -Path D:\powershell\$date -Name ($time + ".txt")

Example 5: To create folder with current time only :

$date= ((Get-Date).ToString('hh mm ss' ))
New-Item -ItemType Directory -Path "D:\Powershell\folder 1\$date"

By AMARNATH K

AM@RNATH is a Technology specialist primarily focus on Microsoft Technologies & Cloud Security. His certification includes M365 Certified: Enterprise Admin Expert. He loves to explore things from the latest technologies to cooking new recipes. Basically from Chennai, the southern part of India. Lucky son, friendly husband, and a proud father who loves to spend time with his family & friends while the laptop is not in his table.