echo is an alias in PowerShell. As you would expect it’s an alias for the closest equivalent to the Linux echo:
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:
Whether these produce any output is controlled by commandline or environment flags.
In bash, echo -n echoes back the string without printing a newline, so if you do this:
you get:
….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:
….then you get this:
Note that -nonewline doesn’t ‘work’ if you’re in the ISE.
The best PowerShell equivalent to egrep or grep is select-string:
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.
Powershell is case-insensitive by default, so:
…would return:blue_flag.txt:3:From Stamford Bridge to Wembley
If you want to do a case sensitive search, then you can use:
The Powershell equivalent to the -v option would be -notmatch
To search for more than one string within a file in bash, you use the syntax:
This will return lines which contain either ‘blue’ or ‘stamford’.
The PowerShell equivalent is to seperate the two strings with a comma, so:
…returns:
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:
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
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.
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:
PowerShell-level variables are given by:
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
The lognames that I typically look at are ‘system’, ‘application’ or ‘security’.
In bash the following changes the prompt when you are at the command line
The Powershell equivalent to this is:function prompt { "$ " }
I found this on Richard Siddaway’s Blog