# store settings here $Path = "HKCU:\software\powertips\settings"
# check whether key exists $exists = Test-Path-Path$Path if ($exists-eq$false) { # if this is first run, initialize registry key $null = New-Item-Path$Path-Force }
# write current date and time to registry $date = Get-Date-Format'yyyy-MM-dd HH:mm:ss' $null = New-ItemProperty-Path$Path-Name LastRun -PropertyType String -Value$date-Force
How would you query for all AD users with names that start with a “e”-“g”? You shouldn’t use a client-side filter such as Where-Object. One thing you can do is use the -Filter parameter with logical operators such as -and and -or:
When you run this chunk of code, you get a list of services in a grid view window, and the first column “ID#” is added with incrementing ID numbers.
The technique can be used to add arbitrary columns. Simply use a hash table with key N[ame] for the column name, and key E[xpression] with the script block that generates the column content.
begin { # if comparison needs to be case-sensitive, use a # case-sensitive hash table, if ($CaseSensitive) { $hash = [System.Collections.Hashtable]::new() } # else, use a default case-insensitive hash table else { $hash = @{} } }
process { foreach ($elementin$InputObject) { # take the key from the property that was requested # via -Property
# if the user submitted a script block, evaluate it if ($Property-is [ScriptBlock]) { $key = & $Property } else { $key = $element.$Property } # convert the key into a string if requested if ($AsString) { $key = "$key" }
# make sure NULL values turn into empty string keys # because NULL keys are illegal if ($key-eq$null) { $key = '' }
# if there was already an element with this key previously, # add this element to the collection if ($hash.ContainsKey($key)) { $null = $hash[$key].Add($element) } # if this was the first occurrence, add a key to the hash table # and store the object inside an arraylist so that objects # with the same key can be added later else { $hash[$key] = [System.Collections.ArrayList]@($element) } } }
end { # default output are objects with properties # Count, Name, Group if ($AsHashTable-eq$false) { foreach ($keyin$hash.Keys) { $content = [Ordered]@{ Count = $hash[$key].Count Name = $key } # include the group only if it was requested if ($NoElement-eq$false) { $content["Group"] = $hash[$key] }
# return the custom object [PSCustomObject]$content } } else { # if a hash table was requested, return the hash table as-is $hash } } }
Get-Process | Group-Object-Property Company Get-Eventlog-LogName System -EntryType Error | Group-Object-Property Source Get-ChildItem-Path c:\windows -File | Group-Object-Property Extension
Basically, the cmdlet builds groups based on the content of a given property. You can also omit the group, and just look at the count if all that matters to you are the number distributions: 基本上,该 cmdlet 基于指定的属性内容创建分组。当您只关注数量分布时,也可以忽略该分组,而只查看总数。
$obj = New-Object-ComObject Shell.Application $browser = $obj.Windows() | Where-Object FullName -like'*iexplore.exe' | # adjust the below to match your URL Where-Object LocationUrl -like'*powershellmagazine.com*' | # take the first browser that matches in case there are # more than one Select-Object-First1