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.
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.
it worked for me , thank you
Thanks its worked to get file name created with date and time
Thanks Dhinesh for sharing.
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
Please share what you have tried. Happy to help you.
New-Item -itemType Directory
New-Item -itemType File
Thank You Richard. I have corrected it.
How would i write something inside the text file? (like a name or smth)