r/PowerShell • • 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.

1 Upvotes

7 comments sorted by

View all comments

2

u/pigers1986 Oct 17 '26

$b disappers as function finishes - it lives only during function execution

i do use

function Get-Hashes{
    [CmdletBinding()]
    [OutputType([string])]
    param(
        [Parameter(Mandatory, Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName)]
        [Alias('FullName')]
        [ValidateNotNullOrEmpty()]
        [string[]]$Path,
 
        [ValidateSet('SHA1', 'SHA256', 'SHA384', 'SHA512', 'MD5')]
        [string]$Algorithm = 'SHA256',
 
        # Return the full details
        [switch]$Detailed
    )
 
    process {
        foreach ($p in $Path) {
            if (-not (Test-Path -LiteralPath $p -PathType Leaf)) {
                Write-Error "File not found or not a file: $p"
                continue
            }
 
            $h = Get-FileHash -LiteralPath $p -Algorithm $Algorithm -ErrorAction Stop
 
            if ($Detailed) { $h } else { $h.Hash }
        }
    }
}

it can loop over more items