A mixed collection of Windows CMD, PowerShell, and netsh/fsutil commands commonly used by SQL Server DBAs for troubleshooting, configuration, and performance tuning.
Usage: Most lines are NOT meant to be run as a single batch file. Copy individual commands into CMD or PowerShell as needed. PowerShell lines should be run in a PS prompt (not cmd.exe). Replace placeholder values before use.
Prerequisites: Administrator rights for most commands, PowerShell 5.1+ recommended, RSAT for cluster/WMI queries, Sysinternals psping.exe for TCP latency testing.
Warnings: Several commands modify system/network settings — test in non-prod first. The empty folder delete command (Section 7) removes folders in-place. The AD password reset via
xp_cmdshell(Section 19) is logged.
@REM Replace 1234 with the port number
netstat -ano | find "1234" | find "LISTEN"tasklist /fi "PID eq 1234".\psping.exe -t SQL01:1433 | ForEach-Object { "{0} - {1}" -f (Get-Date), $_ } | Tee-Object -FilePath "C:\Log\SQL01_log.txt"Runs continuously, timestamps output, and logs to file.
msiexec /i "DataMigrationAssistant.msi" INSTALLFOLDER="D:\Program Files (x86)\DataMigrationAssistant\" /qbnetsh wlan show profile WiFiName key=clearContinuously pings servers and logs failures with timestamps.
$server = @('s1','s2')
while($true) {
$server | ForEach-Object {
if(-not(test-connection $PSItem -Count 1 -Quiet )){
Write-Output "$($PSItem) : $(get-date) - Request Timed Out" |
Out-File c:\_tools\S_log.txt -Append
}
}
}Re-register SQL Server WMI provider (fixes WMI-related SSMS errors). Path may vary by SQL Server version (130 = SQL 2016).
mofcomp "C:\Program Files (x86)\Microsoft SQL Server\130\Shared\sqlmgmproviderxpsp2up.mof"Query Windows Event Logs within a specific time window.
$Begin = Get-Date -Date '12/10/2020 04:30:00'
$End = Get-Date -Date '12/10/2020 05:30:00'
# System log
Get-EventLog -LogName System -After $Begin -Before $End
# Application log
Get-EventLog -LogName Application -After $Begin -Before $End
# Setup log
Get-EventLog -LogName Setup -After $Begin -Before $End
# Security log - failures only
Get-EventLog -LogName Security -After $Begin -Before $End | Where-Object {$_.EntryType -eq 'FailureAudit'} | Out-GridViewWARNING: Run from the correct directory — this deletes empty folders in-place.
echo off
for /f "usebackq delims=" %%d in (`"dir "%~dp0" /ad/b/s | sort /R"`) do rd "%%d"systeminfo | find /i "Boot Time"$PSVersionTable.PSVersion$env:computernameGet-ChildItem "C:\Program Files\Microsoft SQL Server" -Filter *.exe -Recurse | ForEach-Object { $_.FullName }
Get-ChildItem "C:\Program Files (x86)\Microsoft SQL Server" -Filter *.exe -Recurse | ForEach-Object { $_.FullName }Get-ChildItem "K:\" -Filter "*.*" -Recurse -File | Where CreationTime -lt (Get-Date).AddDays(-180) | Select-Object fullname, creationtime | Out-GridViewGet-ChildItem "J:\" -Filter "*.bak" -Recurse -File | Where CreationTime -lt (Get-Date).AddDays(-20) | Select-Object fullname, creationtime, @{N='SizeIngb';E={[double]('{0:N2}' -f ($_.Length/1gb))}} | Out-GridView$path = "\\d$"
$size = 1MB
$limit = 500
$Extension = "*.bak"
$largeSizefiles = Get-ChildItem -Path $path -Recurse -ErrorAction "SilentlyContinue" -Include $Extension |
Where-Object { $_.GetType().Name -eq "FileInfo" } |
Where-Object { $_.Length -gt $size } |
Where-Object { $_.CreationTime -lt (Get-Date).AddDays(-7) } |
Sort-Object -Property Length -Descending |
Select-Object fullname, creationtime, @{Name="SizeInGB";Expression={$_.Length / 1GB}}
$largeSizefiles | Out-GridViewExport all Windows services to CSV for auditing.
Get-WmiObject win32_service | Select-Object Name, DisplayName, State, PathName | Export-Csv -Path "C:\services.csv" -NoTypeInformationUseful for verifying 64K allocation unit size for data/tempdb drives.
fsutil fsinfo ntfsinfo d:\tempdb$wql = "SELECT Label, Blocksize, Name FROM Win32_Volume WHERE FileSystem='NTFS'"
Get-WmiObject -Query $wql -ComputerName '.' | Select-Object Label, Blocksize, NameQuery, disable, and strip 8.3 short names (recommended for SQL data drives). Replace F: with the target drive letter.
FSUTIL.EXE 8dot3name query F:
FSUTIL.EXE 8dot3name set F: 1
fsutil.exe 8dot3name strip /s /v F:Enable WMI firewall rules (required for remote management/monitoring).
netsh advfirewall firewall set rule group="windows management instrumentation (wmi)" new enable=yes@REM Query active sessions on a remote server
query session /server:SERVER_NAME
@REM Reset a disconnected session (replace [ID] with session ID)
reset session [ID] /server:SERVER_NAME
@REM Query users across multiple servers (reads from servers.txt)
for /f %i in (servers.txt) do query user *username* /server:%i
for /f %i in (servers.txt) do query session /server:%inetsh int tcp show globalThese should be disabled on SQL Server nodes per Microsoft best practices.
Netsh int tcp set global chimney=disabled
Netsh int tcp set global netdma=disabled
netsh int ipv4 set global taskoffload=disabledget-cluster | fl *subnet*
(get-cluster).SameSubnetDelay = 2000
(get-cluster).SameSubnetThreshold = 10Replace XXXXXXXXX (current) and YYYYYYYYY (new) with actual values.
Get-ClusterResource "SQL Network Name (XXXXXXXXX)" | ForEach-Object { $_.Name = "YYYYYYYYYYY" }@REM View cluster resources
Get-ClusterResource
@REM Get AG cluster resource parameters
Get-ClusterResource ag01 | Get-ClusterParameter
@REM Disable RegisterAllProvidersIP (use 0 for multi-subnet AG with older clients)
Get-ClusterResource ag01 | Set-ClusterParameter RegisterAllProvidersIP 0
@REM Set DNS TTL to 30 seconds for faster failover client reconnection
Get-ClusterResource ag01 | Set-ClusterParameter HostRecordTTL 30$wc = New-Object System.Net.WebClient
$wc.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentialsFind-Module -Name "azurerm.storage" -Repository "PSGallery" | Save-Module -Path "C:\temp\PSModules\" -Verbose$env:PSModulePathWARNING: This command is logged in SQL error log and Windows security log.
exec xp_cmdshell 'powershell -command "([adsi]''WinNT://Domain/#USERNAME#,user'').ChangePassword(''oldpassword'',''newpassword'')"'Check VMware balloon driver memory reclamation status.
VMWareToolBoxCMD stat balloon