PowerShell Script Tips

You might find the following tips useful when writing scripts to automate your broadcast environment.

Dot Source your scripts

To break up a large script, of place reusable functions in easy to incorporate scripts, you can dot source them by adding the following line in your main script…

. /[script name from current directory]

Note: there is a space between the dot and the slash.

Variables have a scope

If you reference a variable for the first time, it will be available in the scope in which it was created which will be in the following order:

  • Function
  • Script
  • Global
  • Host

If creating a variable (intentionally or otherwise) you can force it to be global. e.g.

if ($global:viseDebug -eq $true)

Will create a global Boolean variable if it doesn’t already exist. If this was used inside a function, it would work as expected the 2nd time the function was called.