Skip to content

Instantly share code, notes, and snippets.

@powercode
Last active December 20, 2016 15:33
Show Gist options
  • Save powercode/c9f7b1b3519c255b0d31533f84dce474 to your computer and use it in GitHub Desktop.
Save powercode/c9f7b1b3519c255b0d31533f84dce474 to your computer and use it in GitHub Desktop.
Sample solutions to practice tasks
# practice tasks
# task1: Get all processes that on your machine where the processname starts with 'po'
Get-Process -name po*
#task 2: Get all processes where the handlecount > 1000
Get-Process | Where-Object HandleCount -gt 1000
#task 3: How many files are there in %windir%\System32
Get-ChildItem -File -LiteralPath $env:windir/System32 | Measure-Object
(Get-ChildItem -File -LiteralPath $env:windir/System32).Length
# task 4: Create a powershell function that returns structured output from du.exe.
class DiskUsageInfo {
[long] $DirectorySize
[long]$CurrentFileSize
[string] $Path
[int]$CurrentFileCount
[int]$FileCount
[int]$DirectoryCount
DiskUsageInfo([long] $DirectorySize, [long]$CurrentFileSize, [string] $Path, [int]$CurrentFileCount, [int]$FileCount, [int]$DirectoryCount) {
$this.DirectorySize = $DirectorySize
$this.CurrentFileSize = $CurrentFileSize
$this.Path = $Path
$this.CurrentFileCount = $CurrentFileCount
$this.FileCount = $FileCount
$this.DirectoryCount = $DirectoryCount
}
}
function Get-DiskUsage {
[Alias('du')]
[OutputType([DiskUsageInfo])]
param(
[Parameter()]
[string] $Path)
if ([string]::IsNullOrEmpty($Path)) {
$Path = $PSCmdlet.SessionState.Path.CurrentFileSystemLocation
}
else {
$Path = $PSCmdlet.GetUnresolvedProviderPathFromPSPath($Path)
}
du.exe -nobanner -q -c -l 1 $Path | Where-Object {$_ -and $_ -notmatch '^processing' } | ConvertFrom-Csv -Delimiter ',' | Foreach-Object {
[DiskUsageInfo]::new($_.DirectorySize, $_.CurrentFileSize, $_.Path, $_.CurrentFileCount, $_.FileCount, $_.DirectoryCount)
}
}
#task 5 : At what line is the occurrence of 'help' in C:\Windows\System32\WindowsPowerShell\v1.0\en-US\default.help.txt that has the highest column number.
Select-String help C:\Windows\System32\WindowsPowerShell\v1.0\en-US\default.help.txt -AllMatches | Sort-Object {$_.Line.LastIndexOf("help")} -Descending | select-object -first 1
Select-String help C:\Windows\System32\WindowsPowerShell\v1.0\en-US\default.help.txt -AllMatches | Sort-Object {$_.Matches[-1].Index} -Descending | select-object -first 1
#task 6: What is the standard deviation of the size of the dlls under System32
# see https://gist.github.com/powercode/a1d450ab6048b54d23672bb36cc39bbc
Get-ChildItem C:\windows\System32\ -file | Foreach-Object Length | Get-StandardDeviation
#task 7: Generate the following text:
<#
'1','2','3','4','5','6','7','8','9','10'
'11','12','13','14','15','16','17','18','19','20'
'21','22','23','24','25','26','27','28','29','30'
'31','32','33','34','35','36','37','38','39','40'
'41','42','43','44','45','46','47','48','49','50'
'51','52','53','54','55','56','57','58','59','60'
'61','62','63','64','65','66','67','68','69','70'
'71','72','73','74','75','76','77','78','79','80'
'81','82','83','84','85','86','87','88','89','90'
'91','92','93','94','95','96','97','98','99','100'
#>
# this is using the 'magic' Foreach method
# see http://www.powershellmagazine.com/2014/10/22/foreach-and-where-magic-methods/
(1..100 | Group-Object {[math]::Floor(($_ - 1)/10)}).Foreach{$_.Group.Foreach{"'$_'"} -join ','}
#task 8: Which two roles in Shakespears Romeo and Juliet has the most lines, and how many words are in those lines?
function Get-RJTopSpeaker {
param($Path, $count)
([xml](Get-Content -Raw $path)).Play.Act.Scene.Speech | Group Speaker | Sort Count -Descending | Select -first $count | Foreach {
[pscustomobject] @{ Actor=$_.Name;SpeechCount = $_.Count; WordCount = ($_.Group | foreach {-split $_.Line} | Measure-Object).Count }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment