The meaning of life is to explore the world

Powershell tips

Posted on By Jason Liu

1. Find cmd parameter needs to be double quoted

E.g.

PS C:\Users\Jason> echo "haha" |find "ha"
FIND: Parameter format not correct
PS C:\Users\Jason> echo "haha" |find '"ha"'
haha

2. Function returns from output

E.g.

# temp.ps1

function f()
{
	echo "aa";
	return "bb";
}

$result = f;
echo ("cc" + $result);
PS C:\Users\Jason\Desktop> .\temp.ps1
ccaa bb

3. Variable scope needs to be explicitly specified

E.g.

# temp.ps1

$v = "global";
function changeV1() { $v = "local-1"; }
function changeV2() { $script:v = "local-2"; }
changeV1;
echo $v;
changeV2;
echo $v;
PS C:\Users\Jason\Desktop> .\temp.ps1
global
local-2