Why Powershell?

by: Finn Kempers

I attended the Powershell course at FLOSSUK Spring Conference 2017. These are my day notes.

Introduction

Windows scripting cmd environment was awful. Python is needlessly complicated. Perl jokes. Shell script is simple. Data flow efficiency is required which good UNIX environments have. Microsoft realised they needed this themselves, which is why Powershell is made. Microsoft are adding bash and SSH (presumably into PowerShell). Office 365 or GUI can manage PowerShell. Registered Microsoft trademark. Scripting language, both usable and powerful.

Covered

Shell basics. Variables, Date & Time, Environment, Files, Scripts and script editors, logic, data and objects, writing scripts, and where next?
Written in mind for researchers handling data.

Commandlets

Command forms:

  • “verb-noun”. Example: “get-childitem”. This makes the commands fairly self-documentative.
  • Write-host “Hello World” (similar to echo)
  • [get-]help write-host (similar to -h or man on linux, man works too as an alias in Powershell)

Variables

Allows to reference values that can change. They will assume values however by typing for example “[float] $a=4”, it will specifically store a variable as a single point floating value. The type of a variable can be verified with “$VARIABLE.getType()”.

  • $a=4 assumed int
  • $b=7.2 assumed float
  • $name=”James”
  • $city=(“X”,”Y”)
  • $Filofax=$null

Still need to use dollar unlike shell script.

Hashes

Perl like to to key value pairs, also called a dictionary. Allows for lookups.

Environment

Get-childitem env:\ list all environment variables, or get a certain username by adding get-childitem env:\username. Typing $username shows username itself.
Environment is inherited from parent process. See Powerpoint for example commands. Environment variable in a child will only be what it is defined in the child, exiting to parent will make it use what it was defined in parent again, as it always has on linux and windows, but before on windows didn’t really access to.

  • Redirection and the pipe:
    • Symbols “>” “<” and “|”
      • It is basically straight up Unix, see the Powerpoint for the example.

Thoughts

Learning PowerShell is very useful for Unix people, because it is basically a Unix environment for Windows and offers great CV opportunity and is a possible new leader in shell environments to take on the current world.

  • “Hello world” | out-file (-append) C:\blah\hello.txt (write file, will overwrite unless appended)
  • Get-content C:\blah\hello.txt (will ping back “hello world” by reading, note also the capitalisation on commands like get-content is optional, “cat” is an alias to “get-content”)
  • $myfilecontents=get-content “filepath” and “$myfilecontent | out-file “filepath” will work on the content in the memory of a computer. Useful for big computers with lots of memory.

Scripts are only touched on here but are useful for frequent use and such. Cmlets can be scripts too, such make your own script that runs after invoking “make-dinner” if one makes such. MSDN has useful articles on learning PowerShell, simple search engine common sense to learning it. http://www.powersearchingwithgoogle.com/ can be used well with PowerShell too and useful. PowerShell is basically a ripoff for the best of Unix environments.

Rich object models, office integration and so on are features only available, when running on Windows. If running on a non-Linux OS, it is basically just a shell.

“foreach ( $file in gci . ){write-host -ForeGroundColor Cyan $file.FullName }” changes text colour. You can however click in windows the PowerShell icon on the window and click properties to change various settings too.

Also available is the Windows PowerShell Integrated Scripting Environment (ISE), which is a environment for making Powershell scripts.

Powershell gives easy scalable power that a lot of unix sysadmins will be familiar with. CSV’s can be imported into PowerShell rather nicely, see 11.3 in the Powershell.pdf.

The latter half of the tutorial was essentially chit chat, bending Powershell in a variety of ways to sate our various curiosities and discussing various technologies, though none hugely relevant to the topic itself. It is worth researching the topic of doing complex custom objects in Powershell:

Notes

Files are provided including the Powerpoint and in PDF form in the drive folder this is located in. It provides also a Powershell.pdf which is a workbook for people to try out. The workbook is also available from:

This is all property of the University of Edinburgh. Under the same site available is a Unix course available too.

About

Finn Kempers is currently studying web technologies at Lancaster and Morecambe College and as an intern developing in Perl, Javascript and Web Frameworks at Shadowcat Systems Limited.

[Note: The views and opinions represented in this review are those of the author and the author alone. FLOSSUK is not responsible for any opinions, comments or values expressed by the author. If you have any concern regarding what has been written you may contact the council at council@flossuk.org.]

Leave a Reply

Your email address will not be published. Required fields are marked *