Skip to content

Instantly share code, notes, and snippets.

Aaron Nelson SQLvariant

Block or report user

Report or block SQLvariant

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
SQLvariant / Export-CSV-Append.ps1
Created Mar 19, 2019
PowerShell proxy cmdlet – function which wraps standard Export-CSV cmdlet but adds handling of the -Append parameter
View Export-CSV-Append.ps1
#Requires -Version 2.0
This Export-CSV behaves exactly like native Export-CSV
However it has one optional switch -Append
Which lets you append new data to existing CSV file: e.g.
Get-Process | Select ProcessName, CPU | Export-CSV processes.csv -Append
For details, see
SQLvariant / Get-MachineInfo.ps1
Created Mar 19, 2019
Simple PowerShell function to retrieve OS info from a Windows machine.
View Get-MachineInfo.ps1
function Get-MachineInfo($ServerName="localhost")
Get-WmiObject win32_computersystem -ComputerName $ServerName |
@{Name="TotalPhysicalMemoryInMB";Expression={"{0:n2}" -f($_.TotalPhysicalMemory/1mb)}},
SQLvariant / Get-History2.ps1
Created Mar 19, 2019
Simple function to give you the duration of all command history in PowerShell
View Get-History2.ps1
function Get-History2
Get-History |
SELECT Id, @{Label="TotalRunningTime";Expression={$_.EndExecutionTime - $_.StartExecutionTime}}, CommandLine, ExecutionStatus, StartExecutionTime, EndExecutionTime
SQLvariant / AdventureWorksDW2017_ExportExcel.ps1
Last active Mar 14, 2019
Export every table in the AdventureWorksDW2017 db to Excel
View AdventureWorksDW2017_ExportExcel.ps1
#Requires -Modules SqlServer
#Requires -Modules ImportExcel
<# The AdventureWorksDW2017 only has 29 tables and they're all under 1 million rows.#>
cd SQLSERVER:\SQL\LocalHost\SQL2017\Databases\AdventureWorksDW2017\Tables
<# Scenario #1 A) all Dimensions in a single file,
and B) each Fact table in their own file. #>
<# A) Every Dimension table in a worksheet named after the table, the same Excel file #>
dir | WHERE { $ -like 'dim*' } |
SQLvariant / ADS_Extension.ps1
Created Mar 14, 2019
Common SQL PowerShell examples for the SqlServer module
View ADS_Extension.ps1
<# Get the Server SMO objects for ServerA & ServerB
The default output for this command will include the Instance name, version, Service Pack, & CU Update Level #>
Get-SqlInstance -ServerInstance ServerA, ServerB
<# Sample Output #>
Instance Name Version ProductLevel UpdateLevel
------------- ------- ------------ -----------
ServerA 13.0.5233 SP2 CU4
ServerB 14.0.3045 RTM CU12
<# Get the Instance name, version, Service Pack, & CU Update Level,
SQLvariant / QuickSQLContainer_wAW2016.ps1
Created Mar 12, 2019
Create a SQL-on-Linux container, and restore the AdventureWorks2016 DB
View QuickSQLContainer_wAW2016.ps1
$BakURL = ""
$BakFile = "$($Home)\Downloads\AdventureWorks2016.bak"
Invoke-WebRequest -Uri $BakURL -OutFile $BakFile
mkdir C:\SQLData\Docker\SQLDev02
#dir C:\SQLData\Docker\SQLDev02 | OGV -PassThru | Remove-Item
Copy-Item -Path "$($Home)\Downloads\AdventureWorks2016.bak" -Destination C:\SQLData\Docker\SQLDev02
<# Create a Container with a volume mounted from the host #>
SQLvariant / ExcelSchemaAndData_to_PowerBI_DataSet.ps1
Created Mar 11, 2019
Use PowerShell to extract Excel Worksheey Schema and Data to create Power BI DataSet
View ExcelSchemaAndData_to_PowerBI_DataSet.ps1
<# Literally one of the oldest SQL PowerShell examples, and it now works on PSCore.
I'm only doing this step to generate data to place in an Excel spreadsheet.
IMPORTANT: If you already have an Excel spreadsheet, skip this section! #>
ls 'SQLSERVER:\SQLRegistration\Database Engine Server Group' -Recurse |
WHERE {$_.Mode -ne 'd' } |
foreach {
Invoke-Sqlcmd -ServerInstance $_.Name -Database master -OutputAs DataTables -Query "
SELECT @@ServerName AS 'ServerName',
DB_NAME(dbid) AS 'DatabaseName',
name AS 'LogicalName',
View Get-DisksSpace.PS1
Function Get-DisksSpace
Grabs Hard Drive & Mount Point space information.
Grabs Hard Drive & Mount Point space information.
.PARAMETER serverName
Accepte 1 or more servernames, up to 50 at once.
View Top15Palatte
"name": "Top15Palatte",
"dataColors": ["#887e6f","#295270","#e6942d","#d52c33","#9f9311","#9fc2dc","#ffc945","#665d3c","#54758D","#DD565C","#EBA957","#9f9f9f","#276B75","#b6749c","#9d9898"
"foreground": "#003052",
"tableAccent": "#9fc2dc"
View 000_Setup
Let's go shopping in the PowerShell Store
Ok, it is not actually called the 'store'
it is called the PowerShell Gallery
<# Use the Search #>
You can’t perform that action at this time.