Skip to content

Commit 33438a2

Browse files
Import published PowerShell guidance scripts
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 120554a commit 33438a2

15 files changed

Lines changed: 814 additions & 0 deletions

.gitattributes

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Needed for publishing of examples, build worker defaults to core.autocrlf=input.
22
* text eol=autocrlf
33

4+
# Preserve the published guidance scripts' upstream LF representation.
5+
guidance/* text eol=lf
6+
47
*.mof text eol=crlf
58
*.sh text eol=lf
69
*.svg eol=lf

guidance/Add-Array.ps1

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
$tests = @{
2+
'PowerShell Explicit Assignment' = {
3+
param($count)
4+
5+
$result = foreach ($i in 1..$count) {
6+
$i
7+
}
8+
$null = $result # just added for linter issues
9+
}
10+
'.Add(..) to List<T>' = {
11+
param($count)
12+
13+
$result = [Collections.Generic.List[int]]::new()
14+
foreach ($i in 1..$count) {
15+
$result.Add($i)
16+
}
17+
}
18+
'+= Operator to Array' = {
19+
param($count)
20+
21+
$result = @()
22+
foreach ($i in 1..$count) {
23+
$result += $i
24+
}
25+
}
26+
}
27+
28+
5kb, 10kb, 100kb | ForEach-Object {
29+
$groupResult = foreach ($test in $tests.GetEnumerator()) {
30+
$ms = (Measure-Command { & $test.Value -Count $_ }).TotalMilliseconds
31+
32+
[pscustomobject]@{
33+
CollectionSize = $_
34+
Test = $test.Key
35+
TotalMilliseconds = [math]::Round($ms, 2)
36+
}
37+
38+
[GC]::Collect()
39+
[GC]::WaitForPendingFinalizers()
40+
}
41+
42+
$groupResult = $groupResult | Sort-Object TotalMilliseconds
43+
$groupResult | Select-Object *, @{
44+
Name = 'RelativeSpeed'
45+
Expression = {
46+
$relativeSpeed = $_.TotalMilliseconds / $groupResult[0].TotalMilliseconds
47+
[math]::Round($relativeSpeed, 2).ToString() + 'x'
48+
}
49+
}
50+
}

guidance/Add-HashTable.ps1

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Adding 10000 items property-style
2+
Measure-Command {
3+
$HashProp = @{}
4+
1..10000 | ForEach-Object { $HashProp.$_ = $_ }
5+
}
6+
7+
# Adding 10000 items using the Add method
8+
Measure-Command {
9+
$HashMethod = @{}
10+
1..10000 | ForEach-Object { $HashMethod.Add($_, $_) }
11+
}
12+
13+
# Adding 10000 items dictionary-style
14+
Measure-Command {
15+
$HashDict = @{}
16+
1..10000 | ForEach-Object { $HashDict[$_] = $_ }
17+
}

guidance/Add-String.ps1

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
$tests = @{
2+
'StringBuilder' = {
3+
$sb = [System.Text.StringBuilder]::new()
4+
foreach ($i in 0..$args[0]) {
5+
$sb = $sb.AppendLine("Iteration $i")
6+
}
7+
$sb.ToString()
8+
}
9+
'Join operator' = {
10+
$string = @(
11+
foreach ($i in 0..$args[0]) {
12+
"Iteration $i"
13+
}
14+
) -join "`n"
15+
$string
16+
}
17+
'Addition Assignment +=' = {
18+
$string = ''
19+
foreach ($i in 0..$args[0]) {
20+
$string += "Iteration $i`n"
21+
}
22+
$string
23+
}
24+
}
25+
26+
10kb, 50kb, 100kb | ForEach-Object {
27+
$groupResult = foreach ($test in $tests.GetEnumerator()) {
28+
$ms = (Measure-Command { & $test.Value $_ }).TotalMilliseconds
29+
30+
[pscustomobject]@{
31+
Iterations = $_
32+
Test = $test.Key
33+
TotalMilliseconds = [math]::Round($ms, 2)
34+
}
35+
36+
[GC]::Collect()
37+
[GC]::WaitForPendingFinalizers()
38+
}
39+
40+
$groupResult = $groupResult | Sort-Object TotalMilliseconds
41+
$groupResult | Select-Object *, @{
42+
Name = 'RelativeSpeed'
43+
Expression = {
44+
$relativeSpeed = $_.TotalMilliseconds / $groupResult[0].TotalMilliseconds
45+
[math]::Round($relativeSpeed, 2).ToString() + 'x'
46+
}
47+
}
48+
}

guidance/Caller.ps1

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
function Invoke-Function4 {
2+
<#
3+
.SYNOPSIS
4+
Demonstrates caller detection using Get-PSCallStack.
5+
#>
6+
[CmdletBinding()]
7+
param()
8+
'In: ' + $MyInvocation.InvocationName
9+
$caller = (Get-PSCallStack)[1].Command
10+
'Caller: ' + $caller
11+
}
12+
13+
function Invoke-Function3 {
14+
<#
15+
.SYNOPSIS
16+
Demonstrates nested caller detection at depth 3.
17+
#>
18+
[CmdletBinding()]
19+
param()
20+
'In: ' + $MyInvocation.InvocationName
21+
$caller = (Get-PSCallStack)[1].Command
22+
'Caller: ' + $caller
23+
Invoke-Function4
24+
}
25+
26+
function Invoke-Function2 {
27+
<#
28+
.SYNOPSIS
29+
Demonstrates nested caller detection at depth 2.
30+
#>
31+
[CmdletBinding()]
32+
param()
33+
'In: ' + $MyInvocation.InvocationName
34+
$caller = (Get-PSCallStack)[1].Command
35+
'Caller: ' + $caller
36+
Invoke-Function3
37+
}
38+
39+
function Invoke-Function1 {
40+
<#
41+
.SYNOPSIS
42+
Entry point demonstrating caller detection through the call stack.
43+
#>
44+
[CmdletBinding()]
45+
param()
46+
'In: ' + $MyInvocation.InvocationName
47+
Get-PSCallStack
48+
$caller = (Get-PSCallStack)[1].Command
49+
'Caller: ' + $caller
50+
Invoke-Function2
51+
}
52+
53+
# Test the functions
54+
Invoke-Function1

guidance/ClassExtension.ps1

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class C {
2+
[string]$Name
3+
4+
C([string]$Name) {
5+
$this.Name = $Name
6+
}
7+
8+
[string]GetInfo() {
9+
return "Name: $($this.Name)"
10+
}
11+
}
12+
13+
class B : C {
14+
[int]$Age
15+
16+
B([string]$Name, [int]$Age) : base($Name) {
17+
$this.Age = $Age
18+
}
19+
20+
[string]GetInfo() {
21+
# Cast $this to the parent class (C) to call its GetInfo()
22+
return "$(([C]$this).GetInfo()), Age: $($this.Age)"
23+
}
24+
}
25+
26+
class A : B {
27+
[string]$Role
28+
29+
A([string]$Name, [int]$Age, [string]$Role) : base($Name, $Age) {
30+
$this.Role = $Role
31+
}
32+
33+
[string]GetInfo() {
34+
# Cast $this to B to call B’s GetInfo(), which itself calls C’s GetInfo()
35+
return "$(([B]$this).GetInfo()), Role: $($this.Role)"
36+
}
37+
}
38+
39+
# Creating and testing an instance
40+
$person = [A]::new('John Doe', 30, 'Manager')
41+
$person.GetInfo()

guidance/Loops.ps1

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
$ranGen = New-Object System.Random
2+
$RepeatCount = 10000
3+
4+
'Basic for-loop = {0}ms' -f (Measure-Command -Expression {
5+
for ($i = 0; $i -lt $RepeatCount; $i++) {
6+
$Null = $ranGen.Next()
7+
}
8+
}).TotalMilliseconds
9+
10+
'Wrapped in a function = {0}ms' -f (Measure-Command -Expression {
11+
function Get-RandNum_Core {
12+
<#
13+
.SYNOPSIS
14+
Gets a random number using a shared Random instance.
15+
#>
16+
param ($ranGen)
17+
$ranGen.Next()
18+
}
19+
20+
for ($i = 0; $i -lt $RepeatCount; $i++) {
21+
$Null = Get-RandNum_Core $ranGen
22+
}
23+
}).TotalMilliseconds
24+
25+
'For-loop in a function = {0}ms' -f (Measure-Command -Expression {
26+
function Get-RandNum_All {
27+
<#
28+
.SYNOPSIS
29+
Gets random numbers in a loop using a shared Random instance.
30+
#>
31+
param ($ranGen)
32+
for ($i = 0; $i -lt $RepeatCount; $i++) {
33+
$Null = $ranGen.Next()
34+
}
35+
}
36+
37+
Get-RandNum_All $ranGen
38+
}).TotalMilliseconds

guidance/Out-Null.ps1

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
$tests = @{
2+
'Assign to $null' = {
3+
$arrayList = [System.Collections.ArrayList]::new()
4+
foreach ($i in 0..$args[0]) {
5+
$null = $arraylist.Add($i)
6+
}
7+
}
8+
'Cast to [void]' = {
9+
$arrayList = [System.Collections.ArrayList]::new()
10+
foreach ($i in 0..$args[0]) {
11+
[void]$arraylist.Add($i)
12+
}
13+
}
14+
'Redirect to $null' = {
15+
$arrayList = [System.Collections.ArrayList]::new()
16+
foreach ($i in 0..$args[0]) {
17+
$arraylist.Add($i) > $null
18+
}
19+
}
20+
'Pipe to Out-Null' = {
21+
$arrayList = [System.Collections.ArrayList]::new()
22+
foreach ($i in 0..$args[0]) {
23+
$arraylist.Add($i) | Out-Null
24+
}
25+
}
26+
}
27+
28+
10kb, 50kb, 100kb | ForEach-Object {
29+
$groupResult = foreach ($test in $tests.GetEnumerator()) {
30+
$ms = (Measure-Command { & $test.Value $_ }).TotalMilliseconds
31+
32+
[pscustomobject]@{
33+
Iterations = $_
34+
Test = $test.Key
35+
TotalMilliseconds = [math]::Round($ms, 2)
36+
}
37+
38+
[GC]::Collect()
39+
[GC]::WaitForPendingFinalizers()
40+
}
41+
42+
$groupResult = $groupResult | Sort-Object TotalMilliseconds
43+
$groupResult | Select-Object *, @{
44+
Name = 'RelativeSpeed'
45+
Expression = {
46+
$relativeSpeed = $_.TotalMilliseconds / $groupResult[0].TotalMilliseconds
47+
[math]::Round($relativeSpeed, 2).ToString() + 'x'
48+
}
49+
}
50+
}

guidance/PSCallStack.ps1

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
function Invoke-Function4 {
2+
<#
3+
.SYNOPSIS
4+
Demonstrates Get-PSCallStack at the deepest call level.
5+
#>
6+
[CmdletBinding()]
7+
param()
8+
'In: ' + $MyInvocation.InvocationName
9+
Get-PSCallStack
10+
}
11+
12+
function Invoke-Function3 {
13+
<#
14+
.SYNOPSIS
15+
Demonstrates Get-PSCallStack at call depth 3.
16+
#>
17+
[CmdletBinding()]
18+
param()
19+
'In: ' + $MyInvocation.InvocationName
20+
Get-PSCallStack
21+
Invoke-Function4
22+
}
23+
24+
function Invoke-Function2 {
25+
<#
26+
.SYNOPSIS
27+
Demonstrates Get-PSCallStack at call depth 2.
28+
#>
29+
[CmdletBinding()]
30+
param()
31+
'In: ' + $MyInvocation.InvocationName
32+
Get-PSCallStack
33+
Invoke-Function3
34+
}
35+
36+
function Invoke-Function1 {
37+
<#
38+
.SYNOPSIS
39+
Entry point demonstrating Get-PSCallStack through nested calls.
40+
#>
41+
[CmdletBinding()]
42+
param()
43+
"In: " + $MyInvocation.InvocationName
44+
Get-PSCallStack
45+
Invoke-Function2
46+
}
47+
48+
# Test the functions
49+
Invoke-Function1

0 commit comments

Comments
 (0)