Restore-SqlBackupFromShare
SYNOPSIS
Restore a SQL Server database from a shared backup location.
SYNTAX
Restore-SqlBackupFromShare [-SourceServer] <String> [-Database] <String> [-TargetServer] <String>
[-RestoreAs] <String> [[-RestoreTime] <DateTime>] [[-RecoveryModel] <String>] [[-FilePath] <String>]
[-NoRecovery] [-WithReplace] [-DisplayConfigOnly] [-OutputScriptOnly] [<CommonParameters>]
DESCRIPTION
This function restores a SQL Server database from a shared backup location. It supports restoring to a specific point in time, setting recovery models, and handling additional tasks such as: - Setting database owners. - Removing orphaned users. - Restoring database security settings. - Adding extended properties to the restored database.
EXAMPLES
EXAMPLE 1
Restore-SqlBackupFromShare -SourceServer "SQLSource" -Database "TestDB" -TargetServer "SQLTarget" -RestoreAs "TestDB_Restored"
Restores the "TestDB" database from "SQLSource" to "SQLTarget" as "TestDB_Restored".
EXAMPLE 2
Restore-SqlBackupFromShare -SourceServer "SQLSource" -Database "ProdDB" -TargetServer "SQLTarget" -RestoreAs "ProdDB_Restored" -RestoreTime "2025-04-01 12:00:00" -WithReplace
Restores the "ProdDB" database from "SQLSource" to "SQLTarget" as "ProdDB_Restored" to a specific point in time, overwriting the existing database.
EXAMPLE 3
Restore-SqlBackupFromShare -SourceServer "SQLSource" -Database "FinanceDB" -TargetServer "SQLTarget" -RestoreAs "FinanceDB_Restored" -FilePath "\\backup\path" -NoRecovery
Restores the "FinanceDB" database from a specified backup path to "SQLTarget" as "FinanceDB_Restored" and leaves it in a restoring state.
PARAMETERS
-SourceServer
The name of the source SQL Server instance where the backup originated.
Type: String
Parameter Sets: (All)
Aliases:
Required: True
Position: 1
Default value: None
Accept pipeline input: True (ByPropertyName, ByValue)
Accept wildcard characters: False
-Database
The name of the source database to restore.
Type: String
Parameter Sets: (All)
Aliases:
Required: True
Position: 2
Default value: None
Accept pipeline input: True (ByPropertyName, ByValue)
Accept wildcard characters: False
-TargetServer
The name of the target SQL Server instance where the database will be restored.
Type: String
Parameter Sets: (All)
Aliases: SqlInstance
Required: True
Position: 3
Default value: None
Accept pipeline input: True (ByPropertyName, ByValue)
Accept wildcard characters: False
-RestoreAs
The name of the database after restoration on the target server.
Type: String
Parameter Sets: (All)
Aliases:
Required: True
Position: 4
Default value: None
Accept pipeline input: True (ByPropertyName, ByValue)
Accept wildcard characters: False
-RestoreTime
The point in time to which the database should be restored. Defaults to 15 minutes ago.
Type: DateTime
Parameter Sets: (All)
Aliases:
Required: False
Position: 5
Default value: [datetime]::Now.AddMinutes(-15)
Accept pipeline input: False
Accept wildcard characters: False
-RecoveryModel
The recovery model to apply to the restored database. Defaults to 'SIMPLE'.
Type: String
Parameter Sets: (All)
Aliases:
Required: False
Position: 6
Default value: SIMPLE
Accept pipeline input: False
Accept wildcard characters: False
-FilePath
The path to the backup files. If not specified, the function will attempt to retrieve backup files from the source server.
Type: String
Parameter Sets: (All)
Aliases:
Required: False
Position: 7
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
-NoRecovery
Indicates whether the database should remain in a restoring state after the restore.
Type: SwitchParameter
Parameter Sets: (All)
Aliases:
Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False
-WithReplace
Indicates whether to overwrite the existing database on the target server.
Type: SwitchParameter
Parameter Sets: (All)
Aliases:
Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False
-DisplayConfigOnly
Displays the restore configuration without performing the restore.
Type: SwitchParameter
Parameter Sets: (All)
Aliases:
Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False
-OutputScriptOnly
Outputs the restore script without executing it.
Type: SwitchParameter
Parameter Sets: (All)
Aliases:
Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False
CommonParameters
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.