r/PowerShell • u/I_see_farts • Feb 16 '26
Any advice on this script?
I've been playing around in Powershell and would like to get some advice on these two scripts I wrote. I've been trying different ParameterSets to see how they work. I also noticed that there's no native convert to / from Base64 / Hex Cmdlets so I thought to make my own.
#region ConvertTo-Type
Function ConvertTo-Type {
[CmdletBinding( DefaultParameterSetName = 'Base64' )]
param (
[Parameter( Mandatory = $true,
Position = 0,
ValueFromPipeline = $true )]
[string]$Value,
[Parameter( ParameterSetName = 'Base64' )]
[switch]$Base64,
[Parameter( ParameterSetName = 'Hex' )]
[switch]$Hex
)
$bytes = [System.Text.Encoding]::UTF8.GetBytes($Value)
Write-Verbose @"
$Value will be encoded UTF8.
"@
$encoding = switch ($PSCmdlet.ParameterSetName) {
Base64 { [convert]::ToBase64String($bytes) }
Hex { [convert]::ToHexString($bytes) }
Default { Throw "Value not selected!" }
}
Write-Verbose @"
Converting to $($PSCmdlet.ParameterSetName).
"@
$encoding
} # End Function
#endregion
#region ConvertFrom-Type
Function ConvertFrom-Type {
[CmdletBinding( DefaultParameterSetName = 'Base64' )]
param (
[Parameter( Mandatory = $true,
Position = 0,
ValueFromPipeline = $true )]
[string]$Value,
[Parameter( ParameterSetName = 'Base64' )]
[switch]$Base64,
[Parameter( ParameterSetName = 'Hex' )]
[switch]$Hex
)
$decoding = switch ($PSCmdlet.ParameterSetName) {
Base64 { [convert]::FromBase64String($Value) }
Hex { [convert]::FromHexString($Value) }
Default { Throw "Value not selected!" }
}
Write-Verbose @"
Converting to $($PSCmdlet.ParameterSetName).
"@
$text = [System.Text.Encoding]::UTF8.GetString($decoding)
Write-Verbose @"
$decoding will be decoded UTF8.
"@
$text
} # End Function
#endregion
Thoughts? Best practices? I didn't write up or include help so it would be shorter.
8
Upvotes
6
u/PinchesTheCrab Feb 16 '26
I feel like there's a lot of extra stuff:
I think this is a cleaner take:
3
u/AdeelAutomates Feb 16 '26 edited Feb 16 '26
I say using .net is using native tools. Its available in PowerShell without any modules needed to add additional functionality.
As such, personally I have no problem just using them directly in my script for simple .net calls like converting To and From Base64 rather than making a function to obfuscate .net being directly called.
2
u/mikenizo808 Feb 17 '26
I just want to say the
-Verbosewith the here string was my favorite part, I will totally use that. Also, I noticed the lack of those cmdlets just like you, and always make some functions similar to what you made.Totally unrelated to yours, I just dug this up and thought you might like it.
6
u/OlivTheFrog Feb 16 '26
There is a module called "convert" on the PSGaller (currently in 1.6.0 version). 35 convert cmdlets (no ConvertTo-Hex cmdlet but a convertTo-Base64 cmdlet)
And in the module called DSInterals (version 6.3) there is also a ConvertTo-Hex cmdlet.
regards
1
u/Due-Skill3084 Feb 16 '26
I would put this line in the .NOTES or .PARAMETER (Value) section of the advanced function's help:
1
u/[deleted] Feb 16 '26 edited Feb 16 '26
[removed] — view removed comment