Skip to main content

Posts

Showing posts from June, 2014

Comparing Optimization of Filtering in PowerShell

This morning in my Hunt Valley, MD PowerShell class, I extended yesterdays lesson (see yesterday’s post) into filtering optimization.  In the PowerShell world, we have a saying: “Filter to the Left”.  That means that you filter out as many objects as possible as close to the beginning of the piped commands as possible.  What we did was use the Get-EventLog cmdlet and filtered it in two ways.  We wanted to filter for Event ID 12.  In the Get-EventLog cmdlet, we used the InstanceID parameter with a value of 12.  In the second execution of Get-EventLog , we piped everything to Where-Object and filtered on the property InstanceID for a value of 12.  We then executed our code from yesterday to test the runtime for each one. # Optimizing for Performance. # Get-Help Get-EventLog -Parameter Newest   # Execute each section individually by highlighting # the code and pressing F8. Clear-History   # This is optimized Get-EventLog -LogName System -InstanceId 12   # This is not O

Using Calculated Properties to get Command Execution Time

We are finishing up day 1 here in my PowerShell class in Hunt Valley, MD.  Just finished up calculated properties and had a request to expand the demonstration that I used a bit.  We were using the Get-History cmdlet to explore calculated properties.  Here is the code and output that we had: Get-History |     Select-Object -Property CommandLine ,     @{N = "ExecutionTime" ;         E = { $_ . EndExecutionTime - $_ . StartExecutionTime}} |     Sort-Object -Property ExecutionTime -Descending |     Select-Object -First 5 CommandLine                               ExecutionTime                            -----------                               -------------                            Show-Command                              00:01:48.0548277                         Update-Help                               00:00:27.2509743                         Get-Process | Select-Object -Property... 00:00:07.7695634                         Get-Service -Name BITS | Stop

How to get PowerShell to Greet You

This will go down as one of my more devious posts. This week my PowerShell class seemed to be having from with my Out-Voice code that I published last year. One of them asked me if PowerShell could say good morning, afternoon, evening to you.  Well, Of course it can.  Since we were about to learn about IF statements, I turned this into an exercise.  To get this to work, you need to accomplish 3 tasks. First, you need to create the script. Second, you need to set up a GPO to launch the script.  Third, set up a GPO to disable the 5 minutes delay in launching user logon scripts in Windows 8.1.  Step 1: Set up the script. You need to make my Out-Voice code available to PowerShell by downloading the code and placing it at the beginning of the logon script.  This will make it available to the local system. In the same script, copy the code below after the Out-Voice code.  Take a moment to look at the help file for Out-Voice.  You can set a female voice if you prefer. 1 2 3 4

How to filter out objects from Get-Volume that do not have a drive letter

While delivering a PowerShell class this week, I utilized the Get-Volume cmdlet to help me demonstrate filtering with Where-Object .  I came across a small problem.  Take a look at this output. PS C:\> Get-Volume | Select -Property DriveLetter, FileSystem, Drivetype                  DriveLetter FileSystem                  DriveType                                ----------- ----------                  ---------                                          D NTFS                        Fixed                                              C NTFS                        Fixed                                              E NTFS                        Fixed                                                NTFS                        Fixed                                                NTFS                        Fixed                                                NTFS                       Fixed      I attempted to filter out all DriveTypes that did not have a drive letter. The fol