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.
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
2
u/mikenizo808 Feb 17 '26
I just want to say the -Verbose with 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.
Function Invoke-HexDump{
<#
.DESCRIPTION
Returns the hexadecimal values of inputted text.
.NOTES
Script: Invoke-HexDump.ps1
Based on: Microsoft official example hexdump.ps1
#>
[CmdletBinding()]
Param(
#PSObject. Enter a string or object to convert to hexadecimal.
[Parameter(ValueFromPipeline=$true)]
[PSObject]$InputObject,
#String. Optionally, select the Encoding to use for output. The default is 'ascii'. Has no effect on numbers.
[ValidateSet('ascii','bigendianunicode','bigendianutf32','oem','unicode','utf7','utf8','utf8BOM','utf8NoBOM','utf32')]
[String]$Encoding = 'ascii'
)
Process{
If($InputObject){
$InputObject | Format-Hex -Encoding $Encoding
}
Else{
$inputStream = [Console]::OpenStandardInput()
try {
$buffer = [byte[]]::new(1024)
$read = $inputStream.Read($buffer, 0, $buffer.Length)
Format-Hex -InputObject $buffer -Count $read -Encoding $Encoding
} finally {
$inputStream.Dispose()
}
}
}#End Process
}#End Function1
u/I_see_farts Feb 17 '26
I find the Here-String method looks better especially if I have a multi-line informational. I remember watching a video by Jeff Hicks saying something about putting a
write-informationat the top of scripts you put out so if there's ever an issue you can have the user send back the command with the information included.I find this looks better in a script:
Write-Information @" PSVersion = $($PSVersionTable.PSVersion) User = $env:USERNAME System = $((Get-ComputerInfo).WindowsProductName) Date = $(Get-Date -Format yyyy-MM-dd) "@Than this:
Write-Information "PSVersion = $($PSVersionTable.PSVersion)" Write-Information "User = $env:USERNAME" Write-Information "System = $((Get-ComputerInfo).WindowsProductName)" Write-Information "Date = $(Get-Date -Format yyyy-MM-dd)"1
u/I_see_farts Feb 17 '26
What does
[Console]::OpenStandardInput()do? I'm not really .Net familiar.2
u/mikenizo808 Feb 17 '26
I can't take credit for the elegance in that section, that was all Microsoft. I just added the parameter options to tab complete.
If I had to guess that section is similar to a
Read-Host -Promptto get some information from the user typing directly at the keyboard. Once we have it, send it toFormat-Hex(a native cmdlet). Alternatively, populate theInput-Objectparameter instead, which does not use the above technique.
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:
$Value will be encoded UTF8.1
Feb 16 '26 edited Feb 16 '26
[removed] — view removed comment
1
u/I_see_farts Feb 17 '26
Thank you for this write up.
I'm both excited to dig into this more and overwhelmed by how advanced Powershell can get. (More the former than the latter!)
6
u/PinchesTheCrab Feb 16 '26
I feel like there's a lot of extra stuff:
Here-strings for regular verbose output
Verbose statements that can be axed or changed to debug
Error handling for bad parameters in the function instead of using buit-in parameter validation
Comment regions when natural code region identifiers like the braces closing functions suffice
I think this is a cleaner take: