Powershell is powerful. But if you forget that it's a scripting language, and approach it as a programming language, you (like me) may be thrown off, time and time again, by its certain... literalness.
I have a script that reads Powershell variable / value pairs from a file we shall call foo.conf . In other words, foo.conf looks like this:
$var1 = "bla1"
$var2 = "bla2"
$var3 = "bla3"
etc.
After reading the contents of this file, it needs to execute those variable assignments. It does it all in this line:
Get-Content("foo.conf") | ForEach-Object { Invoke-Expression($_) }
If foo.conf
has empty lines, those values of $_
will be empty. And when it tries to execute Invoke-Expression($_)
, it will give an error about not being able to invoke a command with an empty argument, or something like that. It's takes time to figure out that the empty argument is due to the empty line in foo.conf
, because, depending on what text editor you use, you may not even notice an empty line at the end of the file. (I use emacs, and it does not show me empty lines. Perhaps there is some mode that would show it, but it didn't even occur to me that I would ever need such a thing.)
You are probably asking, why can't I just dot-source foo.conf
? Well, for that it would have to have a .ps1
extension -- you can't dot-source a file Powershell doesn't recognize as a script. But since this file of name-value pairs is essentially a configuration file, that's a reason to give it a .conf
extension -- maybe all the configuration files in your project have such extensions, and you don't want to deviate from the convention.