Windows powershell scripting

Powershell scripting is not too difficult to pick up and there is a wealth of examples on the internet to help solve any syntax related question.

Simply call the appropriate Powershell “cmdlet” to perform the required task.  The return value can be manipulated directly without being forced to assign intermediate values to a temporary variable.  It is easy enough to manipulate or format the return value.

$datestr = (get-date).tostring('yyyyMMdd')

This example uses the get-date cmdlet to get the current date and format it into reasonably friendly way.


Just like any proper language it is possible to create functions to assist in breaking up program logic.

Powershell has a number of primitives that can be used for creating functions or other program logic.  This list of supported primitive is actually fairly large but here is a smaller subset which covers the usual suspects.INT

  • BOOL

Microsoft has taken a route less traveled with respect to how they define a function.  The parameter types are surrounded with square brackets. In addition, they seem to have gone back to “BASIC” and put dollar signs before each variable.

# write out log info
Function My-Add-Function([float]$firstval,[float]$secondval) 
	$returnval = $firstval + $secondval
	return $returnval

Microsoft allows you to call your user defined functions in the way you would expect.  You simply call the function with the values being passed.


	my-add-function $firstvalue $secondvalue

Yet it is also possible to actually pass in the parameters in any order if you simply give the name of each parameter as they are named in the function.


	my-add-function -secondval $secondvalue -firstval $firstvalue

I don’t know of any other languages or scripts that allow you to call your own functions in this manner.  It is interesting but I don’t see it as an important feature.

It actually took me a while to see what was missing from the function declaration – there is no return type.  This doesn’t mean you cannot return values from functions but how it is done is odd to say the least.

# function GetDateNow returns todays date
Function GetDateNow 
        return $TimeStringNow

Basically all output that occurs in the function is returned.  Looking at this method you would think that the variable TimeStringNow is the only thing being returned and it is because of the return keyword.  This is not the case.

# function GetDateNow returns todays date
Function GetDateNow 

This second function declaration is semantically identical to the first.  The return keyword simply exists so it is possible to exit a function at a specific point, it has no connection with the values being returned.

The one part about the return values that I didn’t mention is that everything that is output in the function (no matter when) will be returned to the caller.

I actually do not like how data is returned from a function but I cannot say that this is completely unique.  This is similar to how Unix shell scripting works if you call a function while capturing the output from that function to a return variable.

function unixfunction 
echo "here at point 1"
echo "here at point 2"
echo "here at point 3"
echo returning 6
echo 6

echo $retval

The output looks like this.

here at point 1 here at point 2 here at point 3 returning 6 6

I don’t really like Powershell scripting but that might just be an anti-Microsoft bias. Using Powershell is actually miles better than using batch scripts from all those years ago. It is really just a matter of taste, if you are a Windows aficionado this is not only a good choice but perhaps the best choice as it comes standard with Windows.

Does it suck?  Well, I guess not.  If you look at this from the perspective of replacing the cmd.exe with Powershell you are probably trading up.  If you want more command line programs that you string together then in my opinion it isn’t really an improvement.


This entry was posted in programming and tagged , . Bookmark the permalink.