r/PowerShell • u/Lithlyo • Oct 16 '26
Question REPOST Issue with $PROFILE function
Hello everyone. I hope you are having a good time.
function gethash {
param([string]$string = "")
if ($string -eq [string]::Empty -or $string -isnot [string])
{
Write-Output "input not valid"
}
else
{
(Get-FileHash -algorithm sha256 $string).Hash | tee -Variable b
}
} I Have this function in my $Profile script. when i run gethash something it returns the hash string as expected, but when I try to print the variable containing the hash i just obtained from the piped command: tee, (alias of Tee-Object) with `$b
it prints nothing, the variable is not saved.
on the other hand, if i copy the exact same command from the $profile function directly through the terminal, $b prints what expected: the hash of the file.
I haven't used this function so much, but I'm pretty sure it worked before today.
Thanks in advance to any who will respond.
View all comments
1
u/jimb2 Oct 20 '26
If your input parameter is defined to be a string you will basically get a string in your function. If it is a different object it will convert to a string. There is no need to test if it is a string.
You do want to test that the file exists. This is the relevant test. You do need to handle an empty string as this will error on the path test.
You do need to clear any old result on error.
Use a global scope variable to save the result so it can be reused outside the function
Depending on how you plan to use the function, you might vary the outputs and return value. It's normal to return the result but it might not be needed for a "command prompt" function if you save a global variable and write the output to screen.
Reusable functions would normally not write to the screen. You want to control output from your main script, not get random output from functions used the script.