Command Detail - I
if-then-else
The bash if-then-elif-else
as per:
...could be rendered in PowerShell as:
if [ -f "$FileName" ]
Testing for the existence of a file in bash is done as follows
In PowerShell this could be[1]
Footnotes
[1] The way I've rendered the PowerShell here isn't great, but I've left it like that for a couple of reasons. First, it shows the similarity between PowerShell and Bash, which I think is encouraging for anyone reading this e-book. Second it allows me make this brief point about using aliases.
echo
is handy. It's short, and it looks like it does the same thing as echo
in Unix, MS-DOS and probably a few other languages besides. It pretty much does...but does echo
alias write-output
which allows you to pipe to other PowerShell commands, or does it alias to write-host
, which doesn't?
I've been using PowerShell for a few years now but I didn't know. I had to look it up. This is extra hassle if you're reading a script, which is one of the reasons that it's usually seen as being better practice in scripts to be explicit by using the full command rather than the alias.
Also, in PowerShell scripts rather than this:
...it would typically be seen as better to format using one of these two alternatives:
or:
Last updated