Over a million developers have joined DZone.

Basics of Powershell ~ Empowering Windows Config/Admin Automation

DZone's Guide to

Basics of Powershell ~ Empowering Windows Config/Admin Automation

· DevOps Zone
Free Resource

The Nexus Suite is uniquely architected for a DevOps native world and creates value early in the development pipeline, provides precise contextual controls at every phase, and accelerates DevOps innovation with automation you can trust. Read how in this ebook.

Basics  of  MS Windows Powershell

A dotNet framework based scripting language to automate the configuration/administration of Microsoft Windows machine.

Powershell is loaded with several cmdlets (special command-lets) acting as a built-in shell utilities to perform different tasks on Windows machine for performing administrative tasks.

Getting Started
Powershell's cmdlets act upon and return objects as a result of any action taken.
These can be used in combination with traditional Windows services like Registry, Net and more.

To try your hands over powershell, access it at  'Start Menu' > 'Accessories' > 'Windows Powershell';
there you'll get mainly a shell from 'Powershell', and an interactive IDE-like shell 'Powershell ISE'.

Using powershell, cmdlets are the main power-source of this Powershell which are discussed briefly below.....

to get a quick /Hello World/ feel of Powershell, you could try on few next steps

* emulating a dos command  'echo' used to print message at console,  
       Write-Host "[get-help write-host] would tell about cmdlet write-host"

* emulating a dos command  'cd' used to change current directory,  
   command:  Set-Location C:\Temp

* emulating a dos command  'mkdir' used to create directory,  
   command:  New-Item -name "ztemp" -type directory -Force

* emulating a dos command 'dir' used to list items in current directory,  
   command:  Get-ChildItem -path C:\Temp

* even the windows ' dir' command can be executed and played with inside powershell.
.....could get a list of cmdlets available in Powershell2.0 ~ http://ss64.com/ps/

[] cmdlets
Command-lets have a specific name format of /'verb'-'noun'/ such as 'Get-ChildItem', 'Get-Help', etc
To know more about any 'cmdlet' (like using man in linux shell), use
command: Get-Help <_cmdlet_> -detailed

[] A Glimpse of system level SuperUser stuff,  as possible on a linux shell

  [+] to get a listing of all System Services,
          command:   Get-Service

  [+] to just get a listing of 'Spooler' named system service, telling its 'name','status' and 'display name'
           command:   Get-Service | Where {$_.name -eq 'Spooler'}

  [+] to get current state of 'Spooler' named system service, whether its running/stopped/paused
           command:   Get-Service | Where {$_.name -eq 'Spooler'} | %{ $_.status }

  [+] to have a Powershell script, check for a system service..... start it if stopped

  $svc_name = "aspnet_state"
  $svc_status = Get-Service | Where {$_.name -eq $svc_name} | %{ $_.status }
  if (-not $svc_status) {
     Write-Host "Error: $svc_name not found"
  elseif ($svc_status -eq "running"){
     Write-Host "status ok $svc_name"
  } else {
     Start-Service $svc_name

  [+] N
ow you can save the script above as any file say 'start_aspnet.ps1', but to execute it as an external script you would need the local system's Execution Policies to be unrestricted for the script.

        It's not advisable to have it un-restricted all the times, so you could pass on the specific modes along-with the script you desire to be run in un-restricted mode. As below.....
          cmd_prompt:> powershell -executionpolicy unrestricted -file ".\start_aspnet.ps1"

helpful links:

Source:  http://justfewtuts.blogspot.com/2011/12/basics-of-powershell-empowering-windows.html

The DevOps Zone is brought to you in partnership with Sonatype Nexus.  See how the Nexus platform infuses precise open source component intelligence into the DevOps pipeline early, everywhere, and at scale. Read how in this ebook


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}