Tuesday, April 22, 2014

PowerShell - how to start


today I am working with power shell and I had no clue :-( so I checked out how to get information about the commands themselves. I found the following commands:

  • Get-Help
    shows the help of the get-help command. Adding a command name as argument shows the help of the command in the current context: e.g.: get-help get-childitem.
  • Get-Command
    shows all commands provided. First argument provides a possibility to filter. e.g.: get-command *-service shows all commands with the end "-service".
  • Get-Member
    shows what items were pipelined out from the last statement e.g.: get-childitem | get-member ... you can see that .NET's System.IO.{File|Directory}Info objects were pushed into the pipeline using get-childitem.
  • Get-help about_*
    shows all entries which are help to power shell concepts.
The setup of a command is "verb"-"dash"-"singular noun" like in get-childitem, so it is easy to split commands with the dash e.g. if searching for commands for "*-servi*". (Power shell is not case-sensitive)
What is another cool feature is the -WhatIf and the -Confirm option which is a inherited property (meaning all items where it makes sense have that option). Every item must have implemented -WhatIf parameter if its execution effects the state of the machine 
e.g.: Remove-Item test.txt -WhatIf DOES NOT remove the file, but what it does is telling the user what would have been done, if -WhatIf wouldn't be set...

Remove-Item test.txt -WhatIf
What if: Performing the operation "Remove File" on target "PATHPATHPATH\test.text".

The confirm option (quite boring against whatif) asks if you are really really sure to do whatever you wanted to do. You can set both preferences by changing $WhatIfPreference or $ConfirmPreference.

... but for me as a starter I use the commands I know by heart e.g.: del instead of remove-item... this seems to be so natural for me and makes it much easier to use power shell as a newbe. I was interested what del does and I found out (using get-alias) that this is mapped to Remove-Item too. So dear power shell developers: aliases were a perfect idea :-). Even dir AND ls works. Power shell seems to be very useful! I am looking forward to learn some more.

kind regards,

No comments: