SQL Server : PowerShell Introduction

This article will give you a feel for what PowerShell can do. There are lots of examples on the net, so you can probably perform many tasks by taking someone else’s examples and modifying them.

PowerShell has lots of command. Open up PowerShell and type the following at the prompt to get a list of them.

get-command

Try a few of the “get-” commands to display information.

get-service
get-process

You can pipe commands together. In this example, we spool the list of processes out to a file.

get-process | out-file c:\test.txt

You can See the methods and properties of a command using the get-member (gm) command.

get-service | get-member
get-service | gm

You can alter the output using format-table (ft) command, adding properties displayed by “get-member”. Use “-auto” option to make the output more compact.

get-service | format-table Name, Status, DisplayName, CanStop -auto
get-service | ft Name, Status, DisplayName, CanStop -auto

You can limit the output using where-object (?).

get-service | where-object{$_.Name -eq "Spooler"}
get-service | ?{$_.Name -eq "Spooler"}

You can pipe many commands together to get the result you want. Here we list some specific information about a service called “Spooler”.

get-service | where-object{$_.Name -eq "Spooler"} | format-table Name, Status, DisplayName, CanStop -auto

When you know how to identify and object, you can call one of its methods. For example, find the service called “Spooler” and call the “Stop” and”Start” methods using foreach-object (%) command.

# Check status
get-service | where-object{$_.Name -eq "Spooler"} | format-table Name, Status, DisplayName, CanStop -auto 
# Stop service
get-service | where-object{$_.Name -eq "Spooler"} | Foreach-object{$_.Stop()}
# Check status
get-service | where-object{$_.Name -eq "Spooler"} | format-table Name, Status, DisplayName, CanStop -auto 
# Start service
get-service | where-object{$_.Name -eq "Spooler"} | %{$_.Start()}
# Check status
get-service | where-object{$_.Name -eq "Spooler"} | format-table Name, Status, DisplayName, CanStop -auto

The psdrive command is really useful. You can list all the drives.

psdrive

Notice the registry is listed as a drive too. You can check the contents of the registry from PowerShell. You can use UNIX or Dos type commands for navigation.

dir hkcu:\software\oracle\virtualbox
ls hkcu:\software\oracle\virtualbox

You can interact directly with SQL Server from PowerShell if you import the relevant module.

import-module sqlps

ls sqlserver:\sql\myserver\default

You can use PowerShell variables so you don’t have to keep repeating paths etc.

$db = "sqlserver:\sql\its-n-sfarm-01\default"
cd $db\databases
ls

If you right-click on the database in Management Studio, you can pick the “Start PowerShell” menu option and you will be taken directly to that database in PowerShell. We can check the tables in the database.

cd tables
ls

As we’ve seen before, we can alter the output of the ls/dir command.

ls | gm

ls | ft name, rowcount -auto

We can get the scripts for the tables and push them to a file.

ls | %{$_.script()} | out-file c:\tables-script.sql

For more information see: