How to Copy Folder and Rename It with Current Date and Time in Powershell?

I am trying to write a powershell script that copies my documents folder to a backups folder that I created in the same directory. my copy-item script looks like this:

copy-item C:\Users\Administrator\Documents\ -destination C:\Users\Administrator\Backups\Backup1\

but instead of Backup1 I want it to say Backup-CurrentDate-Time

I know I need to use the get date function but I don't know how to implement it without getting an error

1 Answer

You can use the Get-Date module and command substitution:

 copy-item .\documents\ -destination .\my-backup-$(Get-Date -format "yyyy_MM_dd_hh_mm_ss")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest