← back to the blog


Get Start with PowerShell

Posted in PowerShell by dake

Information to get start with PowerShell

Information for PowerShell /Reference

List of Tools for editing PowerShell

  • Module Browser for Windows PowerShell ISE
    • Good for PowerShell admin to use
    • Module Browser, built on top of PowerShellGet, is a module management toolkit for PowerShell
    • Require install Windows Management Framework 5.0 preview or latest version on computer
    • This tool is good when you want to just play around PowerShell script and don't want to use default PowerShell console window.
  • Adamdriscoll's PowerShell Tools for Visual Studio
    • Good for PowerShell development at VS Environment
    • This vs extension support following features, which if you want to develop something via PowerShell it's the great tool.
    • 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 Resource to Learn PowerShell

List of Useful Powershell command

  • Get-Help display help information to help you get start

    Get-Help
    
  • Set-ExecutionPolicy will change how you can run script

    // Restricted or All Signed or Remote Signed or Unrestricted
    Set-ExecutionPolicy
    
  • Use Get-ExecutionPolicy to see what's your current computer's policy now

    Get-ExecutionPolicy
    
  • Get-Member Get list of member of the PowerShell object

    Get-Service | Get-Member
    
  • Get list of service running on your computer

    Get-Service
    
  • Get Event Log on your computer

    Get-EventLog
    
  • Get Event Log on your computer

    Get-EventLog
    // list event logs
    Get-EventLog -List
    
  • Get List process from your computer

    Get-Process
    // get all member of get-Process' object
    Get-Process | Get-Member
    // stop specific process, stop mongod process
    Stop-Process -Name mongod
    
  • ConvertTo-HTML convert PowerShell list to HTML

    Get-Process | Convert-HTML > C:\get-process.htm
    
  • Export-CSV export PowerShell list to CSV

    Get-Process | Export-CSV C:\get-process.csv
    
  • Get-Command will display list of command

    Get-Command
    
  • Search in command

    Get-Command
    

Example code of PowerShell

Write Hello World on Windows PowerShell command line

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

Run PowerShell script in command

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

Create PowerShell script to automate process of creating website, application pool and etc

The following example will create the website, application pool and the name will base on the path of 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's info, last bootup time, os architecture and etc by PowerShell script

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

Get last logged on user at computer by PowerShell script

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

List of stuff want to get to know in the future

  1. PowerShell logging, and logging to file?
  2. Ability to modify HostFile at PowerShell?
  3. Load Variable from JSON at PowerShell?
  4. Call TFS command via PowerShell? How do I do these?
    • Login into TFS
    • Setup mapping to local path
    • Get latest from TFS server to local
    • Build solution locally
  5. Create PowerShell UI Interactive with User?
  6. How to execute PowerShell script?
  7. How to execute PowerShell script with TFS Build?
  8. How to create the module, and use that module at PowerShell Script?