|
1 | 1 | filter Remove-HashtableEntry { |
2 | 2 | <# |
3 | 3 | .SYNOPSIS |
4 | | - Removes specific entries from a hashtable based on value, type, or name. |
| 4 | + Removes specific entries from a hashtable based on given criteria. |
5 | 5 |
|
6 | 6 | .DESCRIPTION |
7 | | - This version applies keep filters with the highest precedence. If a key |
8 | | - qualifies based on the provided Keep parameters (KeepTypes and/or KeepKeys), |
9 | | - it is preserved no matter what removal conditions might say. |
| 7 | + This function filters out entries from a hashtable based on different conditions. You can remove keys with |
| 8 | + null or empty values, keys of a specific type, or keys matching certain names. It also allows keeping entries |
| 9 | + based on the opposite criteria. If the `-All` parameter is used, all entries in the hashtable will be removed. |
10 | 10 |
|
11 | | - If no keep filters are provided, the function applies removal conditions: |
12 | | - - NullOrEmptyValues: Remove keys with null or empty values. |
13 | | - - RemoveTypes: Remove keys whose values are of the specified type(s). |
14 | | - - RemoveKeys: Remove keys with the specified name(s). |
| 11 | + .EXAMPLE |
| 12 | + $myHashtable = @{ Name = 'John'; Age = 30; Country = $null } |
| 13 | + $myHashtable | Remove-HashtableEntry -NullOrEmptyValues |
15 | 14 |
|
16 | | - When Keep filters are provided, only keys that match ALL specified keep criteria |
17 | | - will be preserved; keys that do not match are removed regardless of removal settings. |
| 15 | + Output: |
| 16 | + ```powershell |
| 17 | + @{ Name = 'John'; Age = 30 } |
| 18 | + ``` |
18 | 19 |
|
19 | | - At the end, the original hashtable is cleared and repopulated with the filtered results. |
| 20 | + Removes entries with null or empty values from the hashtable. |
20 | 21 |
|
21 | 22 | .EXAMPLE |
22 | | - $ht = @{ |
23 | | - KeepThis = 'Value1' |
24 | | - RemoveThis = 'Delete' |
25 | | - Other = 42 |
26 | | - } |
27 | | - $ht | Remove-HashtableEntry -KeepKeys 'KeepThis' -RemoveKeys 'RemoveThis' |
| 23 | + $myHashtable = @{ Name = 'John'; Age = 30; Active = $true } |
| 24 | + $myHashtable | Remove-HashtableEntry -Types 'Boolean' |
| 25 | +
|
| 26 | + Output: |
| 27 | + ```powershell |
| 28 | + @{ Name = 'John'; Age = 30 } |
| 29 | + ``` |
| 30 | +
|
| 31 | + Removes entries where the value type is Boolean. |
28 | 32 |
|
29 | | - This will keep only the key "KeepThis", regardless of other removal flags. |
| 33 | + .EXAMPLE |
| 34 | + $myHashtable = @{ Name = 'John'; Age = 30; Country = 'USA' } |
| 35 | + $myHashtable | Remove-HashtableEntry -Keys 'Age' |
| 36 | +
|
| 37 | + Output: |
| 38 | + ```powershell |
| 39 | + @{ Name = 'John'; Country = 'USA' } |
| 40 | + ``` |
| 41 | +
|
| 42 | + Removes the key 'Age' from the hashtable. |
30 | 43 |
|
31 | 44 | .OUTPUTS |
32 | | - hashtable |
| 45 | + void |
33 | 46 |
|
34 | 47 | .NOTES |
35 | | - The function modifies the input hashtable in place. |
| 48 | + The function modifies the input hashtable but does not return output. |
| 49 | +
|
| 50 | + .LINK |
| 51 | + https://psmodule.io/Hashtable/Functions/Remove-HashtableEntry/ |
36 | 52 | #> |
37 | 53 | [Diagnostics.CodeAnalysis.SuppressMessageAttribute( |
38 | 54 | 'PSUseShouldProcessForStateChangingFunctions', '', |
|
0 commit comments