Powershell backup-sqldatabase -ExpirationDate

ti1 embracon 0 Reputation points


I'm just having a bad time trying to set the expiration date to a SQL Server backup through powershell SQLSever Module.

My need is that the next backup overwrites the current file in the directory.

I've Tried to achieve that with:

$Expira = (get-date).AddDays(1)

Backup-SqlDatabase -ServerInstance $Instancia -Database $Banco -BackupFile $NomeArquivo -Credential $Credential -BackupAction Database -CompressionOption On -ExpirationDate $Expira -Initialize

It returns 'invalid value to EXPIREDATE param'.

I've tried so many ways of date formatting and can't find anything usefull in forums. The documentation also doesn't presents any example and just says the value for 'ExpirationDate' is a <DateTime>.

The command without -ExpirationDate param works just fine.

A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
1,864 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Andreas Baumgarten 92,401 Reputation points MVP

    Hi @ti1 embracon ,

    with the (get-date).AddDays(1) you get the date and time informationen.

    I am not 100% sure if Backup-SqlDatabase is able to work with the time information.

    You can try this to get the date information only:

    $Expira = (get-date).AddDays(1).Date
    Backup-SqlDatabase -ServerInstance $Instancia -Database $Banco -BackupFile $NomeArquivo -Credential $Credential -BackupAction Database -CompressionOption On -ExpirationDate $Expira -Initialize

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)


    Andreas Baumgarten

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.

    Comments have been turned off. Learn more