how do i create a destination folder with a date appended to it in powershell?

For one the commas do not belong but each section of “code” needs their own subexpression. You are also sticking the date in between the base filename and the file extension, so it looks like you want to insert the date in the file name. Instead, you can adjust it to this.

Get-ChildItem -Path 'C:\API\APIBackups' | ForEach-Object {
    Move-Item -Path $_.FullName -Destination "C:\Users\Admin\Desktop\New folder$((Get-Date).ToString("MMddyyyy"))\"

Unless you’re renaming the file, you don’t need to specify it in the path.

Important Note that if that folder does not exist, you will need to create it first. Otherwise you’ll end up with an extensionless file with that name instead. You could test for the path first, create if it doesn’t exist, then move.

Get-ChildItem -Path 'C:\API\APIBackups' | ForEach-Object {
    $newfolder = "C:\Users\Admin\Desktop\New folder$((Get-Date).ToString("MMddyyyy"))\"
    if(-not(Test-Path $newfolder)){
        $null = New-Item -Path $newfolder -ItemType Directory
    Move-Item -Path $_.FullName -Destination $newfolder

The $null is to hide the output that New-Item creates by default.

A suggestion for improvement would be to use Join-Path for building the new folder path

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top