How to generate filename with date and time in PowerShell

You can useĀ Get-Date like this to generate a date that is compatible with characters allowed in filenames:

Get-Date -UFormat "%Y-%m-%d_%H-%m-%S"

Example output:


In order to generate a complete filename, surround that with $() and prepend/append other parts of your desired filename:

mysqldump-$(Get-Date -UFormat "%Y-%m-%d_%H-%m-%S").sql