Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

echo [uint32]::maxvalue says 'uint32::maxvalue' instead of 4294967295

Tags:

powershell

[DBG]: PS C:\Windows\system32>>> echo [uint32]::maxvalue
[uint32]::maxvalue

and

[DBG]: PS C:\Windows\system32>>> $mv = [uint32]::maxvalue
[DBG]: PS C:\Windows\system32>>> echo $mv
4294967295

I am sure powershell has some perfectly good reason for doing this :-). Is there someway i can tell it not to do it. I am actually passing and int to a function and sometimes I want to pass maxvalue

I know I can do

$mv = [uint32]::maxvalue
MyFunc $mv

I am wondering if there is something like

MyFunc ([uint32]::maxvalue)
MyFunc `[uint32::maxvalue`

etc

like image 726
pm100 Avatar asked Feb 20 '23 21:02

pm100


1 Answers

The reason why is that the echo command is interpreting the argument as a string and not an arbitrary value. Hence in the first example it's printing out the argument literally vs. interpreting it as a value. In the second case though it's being interpreted as an expression and being assigned to a value.

You can reproduce this behavior by creating a function yourself that echos the output to the console.

function example { 
  param ([string]$arg)
  Write-Output $arg
}

example [uint]
> [uint]
example 42
> 42

You can also force the echo function to interpret it as a value by using ()s to specify it's an expression

echo [uint32]::maxvalue
> [uint32]::maxvalue
echo ([uint32]::maxvalue)
> 4294967295
like image 133
JaredPar Avatar answered May 10 '23 06:05

JaredPar