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
    

myenv package update

The Windows Subsystem for Linux home page is set with this command:

export LHOME=”$(wslhome)”

If you don’t have permission to access $LHOME, the home page was previously set to the last directory name which you do have permission to access.

The updated wslhome command prints the correct home page even if you don’t have permission to access it. If access is desired, you must use Administrator privilege to give yourself access to the later directories in the path.

myenv package update

There are three significant changes in myenv (http://ContextKnowledgeSystems.org/download/myenv.zip)

  1. $USER is used for userid. The only place that may cause a problem is wslhome, because the Linux userid need not be the same as the Windows userid. You can fix this problem by putting a modified wslhome command in $HOME/bin.
  2. wslhome uses the latest location (obtained from How-To Geek). The path is established in several steps corresponding to the locations where the command may fail because of permissions. You will have to use the Windows security tab to give yourself permission to access your WSL home directory. If you are not using Ubuntu, you need to change “*Ubuntu*” to match your choice of Linux systems. Remember to treat the Linux files as read-only.
  3. A machome command and MHOME environment variable are added to accommodate macOS, which uses “/Users/userid” instead of “/home/userid”. The mkzip command is changed to use $HOME/tmp because macOS links /tmp to /private/tmp.

myenv package for Windows 10 + Cygwin + WSL/Ubuntu

Windows 10 can run two Linux subsystems simultaneously: Cygwin and Ubuntu. I developed a small package of commands (myenv.zip) to conveniently access files from all three operating systems. It creates five environment variables

myOS — Cygwin or GNU/Linux

myDRIVE — install drive of Windows 10

WHOME — Windows home directory

CHOME — Cygwin home directory

LHOME — Ubuntu home directory

Just add “.  myenv” to your .profile and you can access files like this:

$WHOME/OneDrive     (Microsoft cloud store)

$CHOME/KE/bin/ke.exe     (my Knowledge Explorer)

$LHOME/../../rootfs/etc/shadow     (to delete forgotten password)

There’s one caveat: Microsoft has a complex scheme for buffering $LHOME (Windows file system) and $HOME (Ubuntu file system). Don’t try to access $LHOME until you exit the Ubuntu terminal window.

The myenv package includes three other useful commands:

path — prints each $PATH directory on separate line

wordpad — example command not found in $PATH directory

mkzip — zip all files in directory and its subdirectories

To install the free myenv package, download http://ContextKnowledgeSystems.org/download/myenv.zip and unzip in any directory in $PATH. I suggest using directory $myDRIVE/bin or /usr/local/bin or $HOME/bin.