Thor Logo dbatools

Measure-DbaDiskSpaceRequirement

View Source
Pollus Brodeur (@pollusb)
Windows, Linux, macOS

Synopsis

Calculates disk space requirements for database migration between SQL Server instances

Description

Analyzes database files on source and destination instances to calculate space requirements before migration. Shows file size differences, mount points, and identifies potential overwrites when copying databases between SQL Server instances.

The function compares data and log files from the source database against existing files on the destination, accounting for scenarios where files exist only on source, only on destination, or on both sides. This prevents migration failures due to insufficient disk space and helps plan storage allocation.

Accepts pipeline input with Source, Database, and Destination properties, making it ideal for bulk migration planning from CSV files, SQL queries, or PowerShell objects.

Syntax

Measure-DbaDiskSpaceRequirement
    [-Source] <DbaInstanceParameter>
    [-Database] <String>
    [[-SourceSqlCredential] <PSCredential>]
    [-Destination] <DbaInstanceParameter>
    [[-DestinationDatabase] <String>]
    [[-DestinationSqlCredential] <PSCredential>]
    [[-Credential] <PSCredential>]
    [-EnableException]
    [<CommonParameters>]

 

Examples

 

Example: 1
PS C:\> Measure-DbaDiskSpaceRequirement -Source INSTANCE1 -Database DB1 -Destination INSTANCE2

Calculate space needed for a simple migration with one database with the same name at destination.

Example: 2
PS C:\> @(
>> [PSCustomObject]@{Source='SQL1';Destination='SQL2';Database='DB1'},
>> [PSCustomObject]@{Source='SQL1';Destination='SQL2';Database='DB2'}
>> ) | Measure-DbaDiskSpaceRequirement

Using a PSCustomObject with 2 databases to migrate on SQL2.

Example: 3
PS C:\> Import-Csv -Path .\migration.csv -Delimiter "`t" | Measure-DbaDiskSpaceRequirement | Format-Table -AutoSize

Using a CSV file. You will need to use this header line “SourceDestinationDatabaseDestinationDatabase”.

Example: 4
PS C:\> $qry = "SELECT Source, Destination, Database FROM dbo.Migrations"
PS C:\> Invoke-DbaCmd -SqlInstance DBA -Database Migrations -Query $qry | Measure-DbaDiskSpaceRequirement

Using a SQL table. We are DBA after all!

Required Parameters

-Source

Specifies the source SQL Server instance containing the database to analyze for migration.
This is where the database currently exists and from which file sizes will be measured.

PropertyValue
Alias
RequiredTrue
Pipelinetrue (ByPropertyName)
Default Value
-Database

Specifies the name of the database to analyze on the source instance.
The database must exist on the source server as the function reads actual file sizes from this database.

PropertyValue
Alias
RequiredTrue
Pipelinetrue (ByPropertyName)
Default Value
-Destination

Specifies the destination SQL Server instance where the database will be migrated.
Used to determine target file paths, check for existing databases with the same name, and calculate mount point requirements.

PropertyValue
Alias
RequiredTrue
Pipelinetrue (ByPropertyName)
Default Value

Optional Parameters

-SourceSqlCredential

Login to the target instance using alternative credentials. Accepts PowerShell credentials (Get-Credential).
Windows Authentication, SQL Server Authentication, Active Directory - Password, and Active Directory - Integrated are all supported.
For MFA support, please use Connect-DbaInstance.

PropertyValue
Alias
RequiredFalse
Pipelinetrue (ByPropertyName)
Default Value
-DestinationDatabase

Specifies the database name to use on the destination instance if different from the source database name.
When omitted, the destination database will use the same name as the source database.
Useful when migrating databases that need to be renamed or when avoiding naming conflicts on the destination server.

PropertyValue
Alias
RequiredFalse
Pipelinetrue (ByPropertyName)
Default Value
-DestinationSqlCredential

Login to the target instance using alternative credentials. Accepts PowerShell credentials (Get-Credential).
Windows Authentication, SQL Server Authentication, Active Directory - Password, and Active Directory - Integrated are all supported.
For MFA support, please use Connect-DbaInstance.

PropertyValue
Alias
RequiredFalse
Pipelinetrue (ByPropertyName)
Default Value
-Credential

The credentials to use to connect via CIM/WMI/PowerShell remoting.

PropertyValue
Alias
RequiredFalse
Pipelinetrue (ByPropertyName)
Default Value
-EnableException

By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
This avoids overwhelming you with “sea of red” exceptions, but is inconvenient because it basically disables advanced scripting.
Using this switch turns this “nice by default” feature off and enables you to catch exceptions with your own try/catch.

PropertyValue
Alias
RequiredFalse
Pipelinefalse
Default ValueFalse

Outputs

PSCustomObject

Returns one object per database file analyzed during migration planning. Multiple objects are returned for databases with multiple files (one per file). Objects are returned for three scenarios:

files on both source and destination, files only on source, or files only on destination.

Default display properties (via Select-DefaultView):

  • SourceSqlInstance: The full SQL Server instance name of the source (computer\instance)
  • SourceDatabase: Name of the source database
  • SourceLogicalName: Logical name of the database file on the source
  • SourceFileName: Operating system file path of the source file
  • SourceFileSize: DbaSize object of the source file size (bytes, converts to KB/MB/GB/TB)
  • DestinationComputerName: The computer name of the destination SQL Server instance
  • DestinationSqlInstance: The full SQL Server instance name of the destination (computer\instance)
  • DestinationDatabase: Name of the destination database (may differ from source if renamed)
  • DestinationFileName: Operating system file path of the destination file (null if file only on source)
  • DestinationFileSize: DbaSize object of destination file size; null or 0 if file only on source (displayed as negative value when present)
  • DifferenceSize: DbaSize object showing the difference in file size between source and destination
  • MountPoint: The volume mount point where the destination file is or will be located
  • FileLocation: Scenario description - “Source and Destination”, “Only on Source”, or “Only on Destination”

*Hidden properties (available via Select-Object ):

  • SourceComputerName: The computer name of the source SQL Server instance
  • SourceInstance: The SQL Server instance name of the source
  • DestinationInstance: The SQL Server instance name of the destination
  • DestinationLogicalName: Logical name of the destination file (null if file only on source)
  • SourceDatabaseName: Source database name (used in “Only on Destination” scenario)
  • DestinationDatabaseName: Destination database name (used in “Only on Destination” scenario) Use Select-Object * to access all properties including hidden ones.