To create a folder with a date in its name using PowerShell, you can use the Get-Date cmdlet to retrieve the date, format it appropriately, and then use the New-Item cmdlet with the -Name parameter set to the desired folder name.
The following method shows how you can do it.
Method 1: How to create a folder with current date in its name
# Get the current date
$ date = Get-Date
# Define the desired format for the date string
$format = "dd-MM-yyyy"
# convert the date to the formatted string
$formattedDate = $date.ToString($format)
$ Create a folder with the current date in its name
New-Item -ItemType Directory -Name "Log_$formattedDate" -Force
This example creates a folder with the current date in its name.
The following example shows how to use this method.
Create a Folder with the Current Date in its Name
You can use the following PowerShell script to create a folder with the current date in its name.
# Get the current date $date = Get-Date # Define the desired format for the date string $format = "dd-MM-yyyy" # convert the date to the formatted string $formattedDate = $date.ToString($format) $ Create a folder with the current date in its name New-Item -ItemType Directory -Name "Log_$formattedDate" -Force
Output:
In this script, the $date variable stores the current date. The $format variable stores the format for the date string. We convert the date to the formatted string using the ToString() method and store the value in the $formattedDate variable.
Finally, the New-Item cmdlet is used to create a folder with the name “Log_$formattedDate“, where the $formattedDate variable stores the current date in the “dd-MM-yyyy” format.
After running the above script, it creates a folder with the date in its name.
Conclusion
I hope the above article on creating a folder with the date in the name using PowerShell is helpful to you.
You can find more topics about Active Directory tools and PowerShell basics on the ActiveDirectoryTools home page.