Skip to content

Commit f513a21

Browse files
prelim v2.39.0
1 parent f9006ba commit f513a21

16 files changed

Lines changed: 1378 additions & 16 deletions

PSScriptTools.psd1

6.81 KB
Binary file not shown.

PSScriptTools.psm1

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,23 @@ if ($myinvocation.line -match "-verbose") {
44
}
55
Write-Verbose "Loading public functions"
66

7-
#exclude Get-MyCounter.ps1 because it requires a Windows platform
8-
Get-ChildItem -path $PSScriptRoot\functions\*.ps1 -Exclude 'Get-MyCounter.ps1' |
7+
#exclude files that have special requirements
8+
Get-ChildItem -path $PSScriptRoot\functions\*.ps1 -Exclude 'Get-MyCounter.ps1','Get-FileExtensionInfo.ps1' |
99
ForEach-Object -process {
1010
Write-Verbose $_.fullname
1111
. $_.FullName
1212
}
1313

14+
Write-Verbose "Loading Windows-specific commands"
1415
if ($IsWindows -OR ($PSEdition -eq 'Desktop')) {
1516
. "$PSScriptRoot\functions\Get-MyCounter.ps1"
1617
}
1718

19+
if ($IsCoreCLR) {
20+
Write-Verbose "Loading PowerShell 7 specific commands"
21+
. "$PSScriptRoot\functions\Get-FileExtensionInfo.ps1"
22+
}
23+
1824
Write-Verbose "Define the global PSAnsiFileMap variable"
1925
$json = "psansifilemap.json"
2026

@@ -179,4 +185,6 @@ Function Open-PSScriptToolsHelp {
179185
Write-Warning "Can't find $pdf."
180186
}
181187
Write-Verbose "Ending $($myinvocation.MyCommand)"
182-
}
188+
}
189+
190+
$VerbosePreference = "silentlyContinue"

README.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,12 @@ User Jeff C:\Program Files (x86)\Vale\ True
592592

593593
## File Tools
594594

595+
### [Get-FileExtensionInfo](docs/Get-FileExtensionInfo.md)
596+
597+
This command will search a given directory and produce a report of all files based on their file extension. This command is only available in PowerShell 7. The extension with the largest total size will be highlighted in color.
598+
599+
![Get-FileExtensionInfo](images/gfei.png)
600+
595601
### [Test-EmptyFolder](docs/Test-EmptyFolder.md)
596602

597603
This command will test if a given folder path is empty of all files anywhere in the path. This includes hidden files. The command will return True even if there are empty sub-folders. The default output is True or False but you can use -Passthru to get more information.
@@ -1641,6 +1647,90 @@ TotalMemGB FreeMemGB PctFree
16411647

16421648
## Scripting Tools
16431649

1650+
### [New-PSDynamicParameter](docs/New-PSDynamicParameter)
1651+
1652+
This command will create the code for a dynamic parameter that you can insert into your PowerShell script file. You need to specify a parameter name and a condition. The condition value is code that would run inside an If statement. Use a value like $True if you want to add it later in your scripting editor.
1653+
1654+
```powershell
1655+
PS C:\> New-PSDynamicParameter -Condition "$PSEdition -eq 'Core'" -ParameterName ANSI -Alias color -Comment "Create a parameter to use ANSI if running PowerShell 7" -ParameterType switch
1656+
1657+
DynamicParam {
1658+
# Create a parameter to use ANSI if running PowerShell 7
1659+
If (Core -eq 'Core') {
1660+
1661+
$paramDictionary = New-Object -Type System.Management.Automation.RuntimeDefinedParameterDictionary
1662+
1663+
# Defining parameter attributes
1664+
$attributeCollection = New-Object -Type System.Collections.ObjectModel.Collection[System.Attribute]
1665+
$attributes = New-Object System.Management.Automation.ParameterAttribute
1666+
$attributes.ParameterSetName = '__AllParameterSets'
1667+
$attributeCollection.Add($attributes)
1668+
1669+
# Adding a parameter alias
1670+
$dynalias = New-Object System.Management.Automation.AliasAttribute -ArgumentList 'color'
1671+
$attributeCollection.Add($dynalias)
1672+
1673+
# Defining the runtime parameter
1674+
$dynParam1 = New-Object -Type System.Management.Automation.RuntimeDefinedParameter('ANSI', [Switch], $attributeCollection)
1675+
$paramDictionary.Add('ANSI', $dynParam1)
1676+
1677+
return $paramDictionary
1678+
} # end if
1679+
} #end DynamicParam
1680+
```
1681+
1682+
This creates dynamic parameter code that you can use in a PowerShell function. Normally you would save this output to a file or copy to the clipboard so that you can paste it into scripting editor.
1683+
1684+
You can also use a WPF-based front-end command, [New-PSDynamicParameterForm](docs/New-PSDynamicParameterForm.md). You can enter the values in the form. Required values are indicated by an asterisk.
1685+
1686+
![New-PSDynamicParameterForm](images/new-psdynamicparameter-form.png)
1687+
1688+
Clicking `Create` will generate the dynamic parameter code and copy it to the Windows clipboard. You can then paste it into your scripting editor.
1689+
1690+
```powershell
1691+
DynamicParam {
1692+
1693+
If ($Filter -eq 'domain') {
1694+
1695+
$paramDictionary = New-Object -Type System.Management.Automation.RuntimeDefinedParameterDictionary
1696+
1697+
# Defining parameter attributes
1698+
$attributeCollection = New-Object -Type System.Collections.ObjectModel.Collection[System.Attribute]
1699+
$attributes = New-Object System.Management.Automation.ParameterAttribute
1700+
$attributes.ParameterSetName = '__AllParameterSets'
1701+
$attributes.ValueFromPipelineByPropertyName = $True
1702+
1703+
# Adding ValidatePattern parameter validation
1704+
$value = '^\w+-\w+$'
1705+
$v = New-Object System.Management.Automation.ValidatePatternAttribute($value)
1706+
$AttributeCollection.Add($v)
1707+
1708+
# Adding ValidateNotNullOrEmpty parameter validation
1709+
$v = New-Object System.Management.Automation.ValidateNotNullOrEmptyAttribute
1710+
$AttributeCollection.Add($v)
1711+
$attributeCollection.Add($attributes)
1712+
1713+
# Adding a parameter alias
1714+
$dynalias = New-Object System.Management.Automation.AliasAttribute -ArgumentList 'cn'
1715+
$attributeCollection.Add($dynalias)
1716+
1717+
# Defining the runtime parameter
1718+
$dynParam1 = New-Object -Type System.Management.Automation.RuntimeDefinedParameter('Computername', [String], $attributeCollection)
1719+
$paramDictionary.Add('Computername', $dynParam1)
1720+
1721+
return $paramDictionary
1722+
} # end if
1723+
} #end DynamicParam
1724+
```
1725+
1726+
If you import the PSScriptTools module in the PowerShell ISE, you will get a menu shortcut under Add-Ins.
1727+
1728+
![New-PSDynamicParameter ISE](images/new-psdynamicparameter-ise.png)
1729+
1730+
If you import the module in VS Code using the integrated PowerShell terminal, it will a new command. In the command palette, use `PowerShell: Show Additional Commands from PowerShell Modules".
1731+
1732+
![New-PSDynamicParameter VSCode](images/new-psdynamicparameter-vscode.png)
1733+
16441734
### [Get-PSUnique](docs/Get-PSUnique.md)
16451735

16461736
For the most part, objects you work with in PowerShell are guaranteed to be unique. But you might import data where there is the possibility of duplicate items. Consider this CSV sample.

docs/Get-FileExtensionInfo.md

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
---
2+
external help file: PSScriptTools-help.xml
3+
Module Name: PSScriptTools
4+
online version:
5+
schema: 2.0.0
6+
---
7+
8+
# Get-FileExtensionInfo
9+
10+
## SYNOPSIS
11+
12+
Get a report of files based on their extension.
13+
14+
## SYNTAX
15+
16+
```yaml
17+
Get-FileExtensionInfo [[-Path] <String>] [-Recurse] [-Hidden] [-IncludeFiles] [<CommonParameters>]
18+
```
19+
20+
## DESCRIPTION
21+
22+
This command will search a given directory and produce a report of all files based on their file extension. This command is only available in PowerShell 7.
23+
24+
## EXAMPLES
25+
26+
### Example 1
27+
28+
```powershell
29+
PS C:\> Get-FileExtensionInfo c:\work
30+
31+
Path: C:\work [THINKP1]
32+
33+
Extension Count TotalSize Smallest Average Largest
34+
--------- ----- --------- -------- ------- -------
35+
1 0 0 0 0
36+
.bat 1 122 122 122 122
37+
.bmp 2 14113 4509 7056.5 9604
38+
.csv 7 188085 107 26869.29 129351
39+
.db 3 18432 6144 6144 6144
40+
.gif 1 7110 7110 7110 7110
41+
.htm 1 2586 2586 2586 2586
42+
.html 8 580178 1060 72522.25 238054
43+
.jdh 1 92 92 92 92
44+
.jpb 1 9604 9604 9604 9604
45+
.jpg 2 23827 9604 11913.5 14223
46+
.json 8 366166 546 45770.75 310252
47+
.log 1 6323 6323 6323 6323
48+
.md 2 4031 389 2015.5 3642
49+
.pdf 1 80704 80704 80704 80704
50+
.png 4 47598 1071 11899.5 22700
51+
.ps1 5 2713 64 542.6 1530
52+
.ps1xml 2 5765 2794 2882.5 2971
53+
.psd1 1 7696 7696 7696 7696
54+
.reg 1 8802 8802 8802 8802
55+
.txt 27 332297 7 12307.3 72047
56+
.xml 10 67920544 1584 6792054.4 58504746
57+
.zip 1 13493443 13493443 13493443 13493443
58+
```
59+
60+
The extension with the largest total size will be highlighted in color.
61+
62+
## PARAMETERS
63+
64+
### -Hidden
65+
66+
Include files in hidden folders
67+
68+
```yaml
69+
Type: SwitchParameter
70+
Parameter Sets: (All)
71+
Aliases:
72+
73+
Required: False
74+
Position: Named
75+
Default value: None
76+
Accept pipeline input: False
77+
Accept wildcard characters: False
78+
```
79+
80+
### -IncludeFiles
81+
82+
Add the corresponding collection of files. You can access these items by the Files property.
83+
84+
```yaml
85+
Type: SwitchParameter
86+
Parameter Sets: (All)
87+
Aliases:
88+
89+
Required: False
90+
Position: Named
91+
Default value: None
92+
Accept pipeline input: False
93+
Accept wildcard characters: False
94+
```
95+
96+
### -Path
97+
98+
Specify the root directory path to search
99+
100+
```yaml
101+
Type: String
102+
Parameter Sets: (All)
103+
Aliases:
104+
105+
Required: False
106+
Position: 0
107+
Default value: None
108+
Accept pipeline input: False
109+
Accept wildcard characters: False
110+
```
111+
112+
### -Recurse
113+
114+
Recurse through all folders.
115+
116+
```yaml
117+
Type: SwitchParameter
118+
Parameter Sets: (All)
119+
Aliases:
120+
121+
Required: False
122+
Position: Named
123+
Default value: None
124+
Accept pipeline input: False
125+
Accept wildcard characters: False
126+
```
127+
128+
### CommonParameters
129+
130+
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216).
131+
132+
## INPUTS
133+
134+
### None
135+
136+
## OUTPUTS
137+
138+
### FileExtensionInfo
139+
140+
## NOTES
141+
142+
Learn more about PowerShell: http://jdhitsolutions.com/blog/essential-powershell-resources/
143+
144+
## RELATED LINKS
145+
146+
[Get-FolderSizeInfo](Get-FolderSizeInfo.md)

0 commit comments

Comments
 (0)