r/PowerShell • u/Lithlyo • Oct 16 '26
Question Function Issue in $PROFILE path
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 piped command tee, (alias of Tee-Object) as an input 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.
7
u/dodexahedron Oct 16 '26 edited Oct 16 '26
You are finding out about scope.
Variables in a script have script scope by default, no matter where they appear.
When you write it directly in the shell, script scope is your scope, so those variables stick around.
You would get the same effect if you dot-sourced the script file (which is actually how the profile is loaded at launch).
. SomeFile.ps1is equivalent to pasting the contents of SomeFile.ps1 into the shell. That period makes all the difference (hence "dot-source").When you run a script directly, it runs in a new child scope, which is its script scope.
Read up on the details at
Get-Help about_ScopesNote that dot-sourcing still runs the code you dot-sourced. But it will not be discarded after running. It is basically a means of appending a script to your shell session, which is itself one big script.
Regardless, your function should instead just write the value to the success stream, either with Write-Output $b or just $b at the end. Passing things by letting variables escape a script is a no-no. You can already tee it in the caller's pipeline if you need to tee it, or can use -PipelineVariable for what it looks like you're trying to do.
3
u/BlackV Oct 16 '26 edited Oct 20 '26
Not really sure what you're gaining with your custom function, try
that command achieves the same without the
Tee-Objectand the custom functionadditionally,
your
$stringparameter could/should use parameter validation instead of the conditional logicand really really should be called
-pathor-filepathlike most common cmdletswith the already mentioned scope issues, also think about
$bbeing completely hidden from you, unless you look into the function you'd never know it was therealso is just not a nice variable name
2
u/pigers1986 Oct 17 '26
$b disappers as function finishes - it lives only during function execution
i do use
it can loop over more items
1
u/jimb2 Oct 18 '26 edited Oct 18 '26
If you want a result saved as a variable in the main scope (outside of the function) specify the global scope.
This is very handy for working functions that you use from the command line.
For example, I have an AD user lookup function ADU that I typically use multiple times a day in my identity management job. It lists all users matched by an "anr and" ldap lookup, displays the result on the screen, and saves the list of users and some of the useful properties to a variable $adu for reuse. (There's actually a bunch of these lookup functions that save the result with the variable having the same name as the function.)
finds all users with names matching john* and smith*
This function (set) is dot run from the startup profile like
(These funtions could alternately go in a module. Maybe one day. These are not funtions that I would incoprorate in other code, they are designed as a command line tools only.)
At the end of the function, I save the array result globally:
So the result is available if I want to review it or pipe it into something else.
I think this might be similar to what you are trying to achieve.
1
u/BlackV Oct 16 '26
p.s. formatting, you've used the 3 backtick code fence, that does not work on old.reddit only new.reddit
it'll format it properly OR
Inline code block using backticks
`Single code line`inside normal textSee here for more detail
Thanks