Exports MS SQL script for an object from the given server.
Export-DatabaseObjectScript.ps1 [-Server] <String> [-Database] <String> -Urn <String> [-Schema <String>]
-FilePath <String> [-Encoding <String>] [-Append] [-ScriptingOptions <String[]>]
[-SqlVersion <SqlServerVersion>] [<CommonParameters>]
Export-DatabaseObjectScript.ps1 [-Server] <String> [-Database] <String> -Table <String> [-Schema <String>]
-FilePath <String> [-Encoding <String>] [-Append] [-ScriptingOptions <String[]>]
[-SqlVersion <SqlServerVersion>] [<CommonParameters>]
Export-DatabaseObjectScript.ps1 [-Server] <String> [-Database] <String> -View <String> [-Schema <String>]
-FilePath <String> [-Encoding <String>] [-Append] [-ScriptingOptions <String[]>]
[-SqlVersion <SqlServerVersion>] [<CommonParameters>]
Export-DatabaseObjectScript.ps1 [-Server] <String> [-Database] <String> -StoredProcedure <String>
[-Schema <String>] -FilePath <String> [-Encoding <String>] [-Append] [-ScriptingOptions <String[]>]
[-SqlVersion <SqlServerVersion>] [<CommonParameters>]
Export-DatabaseObjectScript.ps1 [-Server] <String> [-Database] <String> -UserDefinedFunction <String>
[-Schema <String>] -FilePath <String> [-Encoding <String>] [-Append] [-ScriptingOptions <String[]>]
[-SqlVersion <SqlServerVersion>] [<CommonParameters>]
This allows exporting a single database object to a SQL script, rather than a whole database as Export-DatabaseScripts.ps1 does.
It can be particularly useful for creating an object-drop script, with all dependencies.
Export-DatabaseObjectScript.ps1 ServerName\instance AdventureWorks2014 -Table Customer -Schema Sales -FilePath Sales.Customer.sql
Exports table creation script to Sales.Customer.sql as UTF8.
Export-DatabaseObjectScript.ps1 ServerName\instance AdventureWorks2014 -Table Customer -Schema Sales -FilePath DropCustomer.sql ScriptDrops WithDependencies SchemaQualify IncludeDatabaseContext
Exports drop script of Sales.Customer and dependencies to DropCustomer.sql.
The name of the server (and instance) to connect to.
Type: String
Parameter Sets: (All)
Aliases: ServerInstance
Required: True
Position: 1
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
The name of the database to connect to on the server.
Type: String
Parameter Sets: (All)
Aliases: TABLE_CATALOG, ROUTINE_CATALOG
Required: True
Position: 2
Default value: None
Accept pipeline input: True (ByPropertyName)
Accept wildcard characters: False
The Urn of the database object to script. Example: “Server[@Name=’ServerName\Instance’]/Database[@Name=’DatabaseName’]/Table[@Name=’TableName’ and @Schema=’dbo’]”
Type: String
Parameter Sets: Urn
Aliases:
Required: True
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
The unquoted name of the table to script. Resolved using the Schema parameter.
Type: String
Parameter Sets: Table
Aliases: TABLE_NAME
Required: True
Position: Named
Default value: None
Accept pipeline input: True (ByPropertyName)
Accept wildcard characters: False
The unquoted name of the view to script. Resolved using the Schema parameter.
Type: String
Parameter Sets: View
Aliases:
Required: True
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
The unquoted name of the stored procedure to script. Resolved using the Schema parameter.
Type: String
Parameter Sets: StoredProcedure
Aliases: ROUTINE_NAME, Procedure, SProcedure
Required: True
Position: Named
Default value: None
Accept pipeline input: True (ByPropertyName)
Accept wildcard characters: False
The unquoted name of the user defined function to script. Resolved using the Schema parameter.
Type: String
Parameter Sets: UserDefinedFunction
Aliases: UDF, Function
Required: True
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
The unquoted name of the schema to use with the Table, View, StoredProcedure, or UserDefinedFunction parameters. Defaults to dbo.
Type: String
Parameter Sets: (All)
Aliases: TABLE_SCHEMA, ROUTINE_SCHEMA
Required: False
Position: Named
Default value: Dbo
Accept pipeline input: True (ByPropertyName)
Accept wildcard characters: False
The file to export the script to.
Type: String
Parameter Sets: (All)
Aliases:
Required: True
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
The file encoding to use for the SQL scripts.
Type: String
Parameter Sets: (All)
Aliases:
Required: False
Position: Named
Default value: UTF8
Accept pipeline input: False
Accept wildcard characters: False
Indicates the file should be appended to, rather than replaced. Useful when piping a list of objects to be scripted to a file.
Type: SwitchParameter
Parameter Sets: (All)
Aliases:
Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False
Provides a list of boolean SMO ScriptingOptions properties to set to true.
Type: String[]
Parameter Sets: (All)
Aliases:
Required: False
Position: Named
Default value: 'EnforceScriptingOptions ExtendedProperties Permissions DriAll Indexes Triggers ScriptBatchTerminator' -split '\s+'
Accept pipeline input: False
Accept wildcard characters: False
The SQL version to target when scripting. By default, uses the version from the source server. Versions greater than the source server’s version may fail.
Type: SqlServerVersion
Parameter Sets: (All)
Aliases:
Accepted values: Version80, Version90, Version100, Version105, Version110, Version120, Version130, Version140, Version150
Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.
https://msdn.microsoft.com/library/microsoft.sqlserver.management.smo.aspx