my first PowerShell script

On Windows 10, I don’t like navigating through several screens to find what OS build is currently installed on my laptop. Being an expert in the Linux bash shell, I thought it would be easy to write a PowerShell script that is similar to the Linux “uname” command. I was wrong!

  1. The standard suffix for a PowerShell script is “ps1”. The third character is the number one, which in some fonts looks like a lower case letter.
  2. You have to give yourself permission to execute your own script. Open a PowerShell ISE window and display the permissions using:
    Get-ExecutionPolicy  -List
    

    There are five different scopes. You need to set permissions for “LocalMachine” and “CurrentUser” scopes using:

    Set-ExecutionPolicy Unrestricted
    Set-ExecutionPolicy -Scope CurrentUser Unrestricted
    

    You must use Administrator mode to set permissions.

  3. The article, “PowerShell equivalents for common Linux/bash commands” by mathieubuisson.github.io, gives this approximation to the Linux “uname” command:
    Get-CimInstance Win32_OperatingSystem | Select-Object $Properties | Format-Table -AutoSize
    

    Copy the Get-CimInstance pipeline into file “uname.ps1” and make it executable.
    Executing this command in a PowerShell ISE window gives this result:

    PS C:\cygwin64\home\rhmcc\bin> ./uname.ps1
    
    SystemDirectory     Organization BuildNumber RegisteredUser SerialNumber            Version
    ---------------     ------------ ----------- -------------- ------------            -------
    C:\WINDOWS\system32              17763       Windows User   00330-80000-00000-AA454 10.0.17763
  4. If you want to get a nice one-line output like uname produces, you have to pipe the output into the appropriate PowerShell editing commands.
  5. If you try to execute “uname.ps1” by the point-and-click method, you will find that the “Notepad” command is executed (to edit the file). If you try to use the Settings menu to change the command, your only option is get a different command from the Microsoft Store. The Microsoft Store does not have the PowerShell command.
  6. The PowerShell command is in directory
    C:/Windows/System32/WindowsPowerShell/v1.0/
    

    which is already in the PATH environment variable. You can use cmd.exe or bash (in WSL or Cygwin or Msys) to execute powershell.exe.

  7. You can find more details by searching
    http://docs.microsoft.com/en-us/powershell
    

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s