PowerShell Versions

advertisement
PowerShell Versions
PowerShell 1.0
PowerShell 1.0 was released in 2006 for Windows XP SP2, Windows Server 2003 and Windows Vista. It is an optional
component of Windows Server 2008.
PowerShell 2.0
PowerShell 2.0 is integrated with Windows 7 and Windows Server 2008 R2 and is released for Windows XP with Service
Pack 3, Windows Server 2003 with Service Pack 2, and Windows Vista with Service Pack 1.
PowerShell V2 includes changes to the scripting language and hosting API, in addition to including more than 240 new
cmdlets
PowerShell 3.0
PowerShell 3.0 is integrated with Windows 8 and with Windows Server 2012. Microsoft has also made PowerShell 3.0
available for Windows 7 with Service Pack 1, for Windows Server 2008 with Service Pack 1, and for Windows Server 2008
R2 with Service Pack 1.
PowerShell 3.0 is part of a larger package, Windows Management Framework 3.0 (WMF3), which also contains the
WinRM service to support remoting.
PowerShell 4.0
PowerShell 4.0 is integrated with Windows 8.1 and with Windows Server 2012 R2. Microsoft has also made PowerShell
4.0 available for Windows 7 SP1, Windows Server 2008 R2 SP1 and Windows Server 2012.
PowerShell 5.0
An initial public preview of PowerShell 5.0 was made available with Windows Management Framework 5.0 (WMF5) on
April 3, 2014
Why does it really matter what version of PowerShell that I am running?
Performance –
I was asked to figure out a way to determine if a file had the attribute of “offline” and produce a list of those files. I ran
the following command on a Windows 7 SP1 machine against a network share
Get-ChildItem "<networkshare>" -recurse | Where-Object {$_.Attributes -like '*Archive*'} | Export-Csv
"C:\work\OfflineFilesmycdrive_ps3.csv"
It took 30 minutes and found 193,381 results
I personally thought it was really slow for what it was doing so I Googled it and I found the following post
http://blogs.msdn.com/b/powershell/archive/2009/11/04/why-is-get-childitem-so-slow.aspx
When we do a directory listing, we show the standard attributes of the file or directory: Mode, LastWriteTime, Length,
and Name. The core Windows API is highly optimized for this basic scenario, and returns these attributes by default along
with the rest of the file information. However, the .NET Framework doesn’t take advantage of this data, and instead goes
back to the network location to ask for all of the file attributes. This chatty behaviour adds a handful of network round
trips for each file or directory, making the directory listing many times slower: hundreds or thousands of times slower in
many cases.
I found another post that said PowerShell 3.0 is faster for the ‘Get-Childitem’ command so I downloaded it and re-ran
my test.
This time it only took 25 minutes and came back with 194,039 results. Running with PowerShell 3.0 was about 16%
faster in the tests I was doing.
New Commands – with each new version, new commands and features have been added
How do I tell what version am I running?
Get-host
$PSVersiontable.PSVersion
Running the same commands on a Windows 2008 Server
CMD in PowerShell
You can stop using the command prompt and start doing all those same tasks within PowerShell
Dir
Systeminfo
Net statistics server (command I would run previously)
Using the PowerShell ISE
Windows PowerShell ISE is a host application that enables you to write, run, and test scripts and modules in a graphical
and intuitive environment. Key features in Windows PowerShell ISE include syntax-coloring, tab completion, Intellisense,
visual debugging, Unicode compliance, and context-sensitive Help. It provides a rich scripting experience.
<not available on Server Core as it requires a user interface>
Run as Administrator!
Zoom
Intellisense
Press F1 on an item for help
Display output in different colours
System Commands
PowerShell is WMI aware
Get-WmiObject (gwmi)
Gwmi can be run against remote computers
Gwmi –computername localhost win32_logicaldisk
Kill a process in PowerShell
Get- process
Get-process spoolsv
Stop-process 1188
Services
Get-Service
Get-Service | where-object {$_.status -eq 'Running'}
Restart-Service <servicename>
Restart-Service spooler
Winrm (Windows Remote Management)
Command to check if its working from a remote machine (using domain admin cmd prompt)
winrm get winrm/config
winrm get winrm/config –r:localhost
To disable it on the machine run
winrm delete winrm/config/Listener?Address=*+Transport=HTTP
To enable it on the machine run
winrm quickconfig
AD Commands
Get most common information
Get-aduser test1
Get all user properties
Get-aduser test1 -properties *
Get most common information and your specific property
Get-aduser test1 -properties PasswordExpired
Format Table
Get-aduser test1 -properties * |ft
Format List
Get-aduser test1 -properties * |fl
Get specific properties
Get-aduser test1 -properties * |fl Name, PasswordExpired
More efficient way to get specific properties
Get-aduser test1 -properties name, passwordexpired |fl Name, PasswordExpired
Exporting Information to a CSV file
Get-aduser test1 | Export-Csv C:\ATS\test1.txt
List all servers in AD
Get-ADComputer -Filter {OperatingSystem -Like "Windows *Server*"} -Properties * | select-object
Name,OperatingSystem,OperatingSystemServicePack
List all servers with a logon data older than X period of time
$logonDate = New-Object System.DateTime(2015,02,22)
Get-ADComputer -Filter {OperatingSystem -Like "Windows *Server*" -and LastLogonTimeStamp -lt
$logonDate} -Property * | Select-object Name,@{Name="Stamp";
Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp)}}
Ping a list of servers to see if they respond
$Computers = Get-Content C:\ATS\list.txt
Test-Connection $Computers -count 1
WhatIF Delete
Get-Content 'C:\Work\Scripts\Delete Servers from AD\Server accounts to be removed from AD.txt' | %
{ Get-ADComputer -Filter { Name -eq $_ } } | Remove-ADObject -Recursive –whatif
By using the –WhatIf command you can test your command to see what the results will be. This is handy if you aren’t
sure that your script will delete the right stuff in production or if you want some documentation on what will be deleted
before you delete it
List Servers
Get-ADComputer -Filter {OperatingSystem -Like "Windows *Server*"} -Properties * | select-object
Name
Remove a list of servers from AD
Get-Content 'C:\Work\Scripts\Delete Servers from AD\Server accounts to be removed from AD.txt' | %
{ Get-ADComputer -Filter { Name -eq $_ } } | Remove-ADObject -Recursive
List Servers
Get-ADComputer -Filter {OperatingSystem -Like "Windows *Server*"} -Properties * | select-object
Name
Other Commands
Count items
(Get-ADComputer -Filter {OperatingSystem -Like "Windows *Server*"} -Properties * | select-object
Name).count
Insert timestamps into PowerShell Outputs
Get-date –format g
Text to Speech
(New-Object –ComObject SAPI.SPVoice).Speak(“This is a test”)
http://ammonsonline.com/teaching-powershell-to-speak/
More details on get-Childitem
get-childitem | where-object {$_.Attributes -like '*archive*’}
get-childitem | where-object {$_.Attributes -like '*directory*’}
Get-ChildItem "C:\ATS" -recurse | Where-Object {$_.Attributes -like '*offline*'}
get-childitem C:\ATS | where-object {$_.Extension –eq “.csv”}
Show only names
Get-ChildItem C:\ -name
Download