PowerShell Studies

These are just notes from my self-studies…maybe something in here will be useful to you.

 

Wednesday, 11/23/2016
Signing Scripts
It’s best to use AllSigned, once you have a certificate. But, RemoteSigned is common. It’s not good to use Bypass or Unrestricted.

Variables
$MyVar=”Hello”
$MyVar=Get-Service bits
$MyVar.status
$MyVar.stop()
$MyVar.refresh()
$MyVar.status

$var=Read-Host “Enter a computer name”
Get-Service -name bits -ComputerName $var

Write-Host $var -ForegroundColor red -Backgroundcolor green
* Write-Host will not send data across the pipeline

Write-Output $var will work with the pipeline

Write-Warning “There is an error”
Write-Error

icm is Invoke-Command
icm -comp PcName {Write-Output $var}

$sessions=New-PSSession -ComputerName PcName
icm -Session $sessions {$var=2}
icm -Session $sessions {$var}

$servers | foreach{Copy-Item c:\default.htm -Destination \\$_\c$\inetpub\wwwroot}

Passing a parameter from the PowerShell console, the script should have
[CmdletBinding()]
param(
            [Parameter(Mandatory=$True)]
            [string[]]$ComputerName
)
Get-WmiObject -ComputerName $ComputerName -class win32_logicaldisk -filter “DeviceID=’c:'” | Select @{n=’freegb’;e={$_.freespace /1gb -as [int]}}

Block comment
<#
#>

Control-J brings up snippets in ISE.

Tuesday, 11/22/2016
Reading in Windows PowerShell Best Practices (76/858)

Custom columns, calculated properties (with hash table)

Example
Get-ADComputer -filter * | Select -Property name, @{name=’ComputerName’;expression={$_.name}}
or
Get-ADComputer -filter * | Select -Property name, @{n=’ComputerName’;e={$_.name}}

Test it with
Get-ADComputer -filter * | Select -Property  @{name=’ComputerName’;expression={$_.name}} | gm

Monday, 11/21/2016
Reading in Windows PowerShell Best Practices (56/858)
Reading in Windows PowerShell Cookbook (66/673)

Unlocking AD accounts
Search-ADAccount -LockedOut
Search-ADAccount -LockedOut | Unlock-ADAccount
Search-ADAccount -LockedOut

Find disabled users
Get-ADUser -Filter ‘enabled -eq $false’ -Server YourDC

Return only functions or cmdlets
Get-Command -CommandType function
Get-Command -CommandType cmdlet

Thursday, 10/27/2016
For Help, one of the best, first commands you should learn), go to the PowerShell console, type
help *

then, pick any item that is displayed and use help again
help Get-Service

then, to see examples, type
help Get-Service -examples

To view help online, type
help Get-Service -online

To view help in window, type
help Get-Service -ShowWindow

* you just saved hundreds of dollars on buying books

For Verbs, return the PowerShell verbs (‘| measure’ returns count)
Get-Verb
Get-Verb| measure

Wednesday, 10/26/2016

End process
Get-Process -Name notepad | Stop-Process [-Confirm]

Check PS Version
$PSVersionTable.PSVersion