Introduction to PowerShell for Unix People by Powershell.org - HTML preview

PLEASE NOTE: This is an HTML preview only and some elements such as links or page numbers may be incorrect.
Download the book in PDF, ePub, Kindle for a complete version.

8. commands detail - e

echo

echo is an alias in PowerShell. As you would expect it’s an alias for the closest equivalent to the Linux echo:

  • write-output

You use it as follows:write-output "Blue is the colour"

As well as write-output there are a couple of options for use in Powershell scripts and functions:

  • write-debug
  • write-verbose

Whether these produce any output is controlled by commandline or environment flags.

echo -n

In bash, echo -n echoes back the string without printing a newline, so if you do this:

img107.png

you get:

img108.png

….with your cursor ending up on the same line as the output, just after the dollar prompt

Powershell has an exact equivalent of ‘echo -n’. If you type:

img109.png

….then you get this:

img110.png

Note that -nonewline doesn’t ‘work’ if you’re in the ISE.

egrep

The best PowerShell equivalent to egrep or grep is select-string:

img111.png

A nice feature of select-string which isn’t available in grep is the -context option. The -context switch allows you to see a specified number of lines either side of the matching one. I think this is similar to SEARCH /WINDOW option in DCL.

egrep -i

Powershell is case-insensitive by default, so:

img112.png

…would return:blue_flag.txt:3:From Stamford Bridge to Wembley

If you want to do a case sensitive search, then you can use:

img113.png

egrep -v

The Powershell equivalent to the -v option would be -notmatch

img114.png

egrep ‘this|that’

To search for more than one string within a file in bash, you use the syntax:

img115.png

This will return lines which contain either ‘blue’ or ‘stamford’.

The PowerShell equivalent is to seperate the two strings with a comma, so:

img116.png

…returns:

img117.png

| egrep -i sql

This is an interesting one, in that it points up a conceptual difference between PowerShell and Bash.

In bash, if you want to pipe into a grep, you would do this:

img118.png

This would show you all the processes which include the string ‘sql’ somewhere in the line returned by ps. The egrep is searching across the whole line. If the username is ‘mr_sql’ then a line would be returned, and if the process is ‘sqlplus’ than a line would also be returned.

To do something similar in PowerShell you would do something more specific

img119.png

So the string ‘sql’ has to match the contents of the property processname. As it happens, get-process by default only returns one text field, so in this case it’s relatively academic, but hopefully it illustrates the point.

env

The Linux ‘env’ shows all the environment variables.

In PowerShell there are two set of environment variables:

- windows-level variables and

- Powershell-level variable

Windows-level variables are given by:

img120.png

PowerShell-level variables are given by:

img121.png

errpt

I think errpt is possibly just an AIX thing (the linux equivalent is, I think, looking at /var/log/message). It shows system error and log messages.

The PowerShell equivalent would be to look at the Windows eventlog, as follows

img122.png

The lognames that I typically look at are ‘system’, ‘application’ or ‘security’.

export PS1=“$”

In bash the following changes the prompt when you are at the command line

img123.png

The Powershell equivalent to this is:function prompt {  "$ "  }

I found this on Richard Siddaway’s Blog