Getting Started with PowerShell

2016/08/035 min read
bookmark this
Responsive image

Table of Contents

  1. Information for PowerShell Reference
  2. List of Tools for Editing PowerShell
  3. List of Resources to Learn PowerShell
  4. List of Useful PowerShell Commands
  5. Example Code
  6. PowerShell Scripts
  7. Future Topics

Introduction

This post provides information to get started with PowerShell, including reference material, editing tools, learning resources, and useful commands with examples.

Information for PowerShell Reference

  • PowerShell Intellisense
  • Book - Effective Windows PowerShell
  • PowerShell Tools for Visual Studio 2015
  • PowerShell Tools for Visual Studio Code
  • PowerShell CookBook
  • Create IIS Website

List of Tools for Editing PowerShell

  • Module Browser for Windows PowerShell ISE

    • Good for PowerShell admins to use
    • Module Browser, built on top of PowerShellGet, is a module management toolkit for PowerShell
    • Requires Windows Management Framework 5.0 preview or the latest version installed on the computer
    • This tool is good when you want to play around with PowerShell scripts and don't want to use the default PowerShell console window.
  • Adamdriscoll's PowerShell Tools for Visual Studio

    • Good for PowerShell development in the VS environment
    • This VS extension supports the following features, making it a great tool for PowerShell development:
      • Syntax Highlighting with customizable fonts and colors
      • IntelliSense for variables, cmdlets, and arguments
      • PowerShell Project Template
      • PowerShell MSBuild Project Options
      • Debugging (Stepping, Locals Support, Call Stack Support, Data Tips)
  • Testing - https://github.com/pester/Pester

  • Use Windows PowerShell Web Access - Good for remote access via the web

  • PowerShell Tools for Visual Studio 2015

List of Resources to Learn PowerShell

  • Video - Getting Started with PowerShell 3.0 Jump Start
  • E-Book - PowerShell with Dr. Tobias Weltner

List of Useful PowerShell Commands

Get-Help

Display help information to help you get started.

Get-Help

Set-ExecutionPolicy

Change how you can run scripts.

// Restricted or AllSigned or RemoteSigned or Unrestricted
Set-ExecutionPolicy

Get-ExecutionPolicy

Use Get-ExecutionPolicy to see what your current computer's policy is.

Get-ExecutionPolicy

Get-Member

Get the list of members of a PowerShell object.

Get-Service | Get-Member

Get-Service

Get the list of services running on your computer.

Get-Service

Get-EventLog

Get event logs on your computer.

Get-EventLog
// list event logs
Get-EventLog -List

Get-Process

Get the list of processes from your computer.

Get-Process
// get all members of Get-Process' object
Get-Process | Get-Member
// stop a specific process, e.g. stop mongod process
Stop-Process -Name mongod

ConvertTo-HTML

Convert a PowerShell list to HTML.

Get-Process | ConvertTo-HTML > C:\get-process.htm

Export-CSV

Export a PowerShell list to CSV.

Get-Process | Export-CSV C:\get-process.csv

Get-Command

Display a list of commands.

Get-Command

Search in Commands

Get-Command

Example Code

Write Hello World on PowerShell Command Line

Write-Host 'Hello world'
// change color
Write-Host -backgroundColor Red "this should be red"

Run PowerShell Script from Command Line

invoke-expression "c:\ps_test\test.ps1"

PowerShell Scripts

Create IIS Website, Application Pool, and More

The following example will create the website and application pool. The name will be based on the path of the script folder.

# CREATE SAMPLE IIS SITE
IMPORT-MODULE WEBADMINISTRATION
$IISAPPPOOLNAME = "SomeApp2"
$IISAPPPOOLDOTNETVERSION = "V4.0"
$IISAPPNAME = "SomeApp2"
$DIRECTORYPATH = "C:\Test\SomeApp2"

$CURRENT_FOLDER_FULLNAME = (get-item -path ".\" -verbose).fullname
$CURRENT_FOLDER_NAME = (get-item -path ".\" -verbose).name

write-host $CURRENT_FOLDER_NAME
$IISAPPPOOLNAME  = $CURRENT_FOLDER_NAME
$IISAPPNAME  = $CURRENT_FOLDER_NAME

#NAVIGATE TO THE APP POOLS ROOT
CD IIS:\APPPOOLS\

#CHECK if the site directory path exists
IF (!(TEST-PATH $DIRECTORYPATH))
{
	# then create the directory folder
	# mkdir will create any parent folders as well
	mkdir $DIRECTORYPATH
}


#CHECK IF THE APP POOL EXISTS
IF (!(TEST-PATH $IISAPPPOOLNAME -PATHTYPE CONTAINER))
{
    #CREATE THE APP POOL
    $APPPOOL = NEW-ITEM $IISAPPPOOLNAME
    $APPPOOL | SET-ITEMPROPERTY -NAME "MANAGEDRUNTIMEVERSION" -VALUE $IISAPPPOOLDOTNETVERSION
	Write-Host 'Create IIS Application Pool : ' + $IISAPPPOOLNAME
}

#NAVIGATE TO THE SITES ROOT
CD IIS:\SITES\

#CHECK IF THE SITE EXISTS
IF (TEST-PATH $IISAPPNAME -PATHTYPE CONTAINER)
{
	$currentPath = (Get-Item $IISAPPNAME).PhysicalPath
	IF ($currentPath -eq $DIRECTORYPATH)
	{
		Write-Host 'same path'
	}
	else
	{
		#Write-Host "need to change physical path from " + $currentPath + " to "  + $DIRECTORYPATH
		$IISAPP = GET-ITEM $IISAPPNAME
		#$IISAPP | SET-ITEMPROPERTY -NAME "PHYSICALPATH" -VALUE $DIRECTORYPATH
		# TODO: this will change physical path, however when click site explore will go to previous path, might have some cache need to clear out
		set-itemproperty $IISAPPNAME -name physicalPath -value $DIRECTORYPATH
	}

    RETURN
}

#CREATE THE SITE
$IISAPP = NEW-ITEM $IISAPPNAME -BINDINGS @{PROTOCOL="HTTP";BINDINGINFORMATION=":80:" + $IISAPPNAME} -PHYSICALPATH $DIRECTORYPATH
$IISAPP | SET-ITEMPROPERTY -NAME "APPLICATIONPOOL" -VALUE $IISAPPPOOLNAME

cd $CURRENT_FOLDER_FULLNAME

Get Computer Info

Get the computer's info, last boot-up time, OS architecture, and more using PowerShell.

Get-WmiObject -class Win32_OperatingSystem -ComputerName localhost | Select-Object -Property CSName, LastBootUpTime, OSType, OSArchitecture, NumberOfUsers

Get Last Logged On User

Get-WmiObject win32_LoggedOnUser -ComputerName localhost | select antecedent -Unique

Future Topics

  1. PowerShell logging and logging to a file
  2. Ability to modify the HostFile with PowerShell
  3. Load variables from JSON in PowerShell
  4. Call TFS commands via PowerShell
  5. Log into TFS
  6. Set up mapping to a local path
  7. Get latest from TFS server to local
  8. Build a solution locally
  9. Create a PowerShell UI for user interaction
  10. How to execute a PowerShell script
  11. How to execute a PowerShell script with TFS Build
  12. How to create a module and use it in a PowerShell script

Conclusion

PowerShell is a powerful scripting language for Windows administration and automation. With the right tools and resources, you can automate tasks like IIS website creation, process management, and system monitoring. Start with the basic commands listed above and gradually explore more advanced scripting techniques.