Summary: Ed Wilson, Microsoft Scripting Guy, talks about the NoNewLine parameter in Windows PowerShell 5.0 in Windows 10.
Microsoft Scripting Guy, Ed Wilson, is here. In addition to new cmdlets in Windows PowerShell 5.0, there are also improvements to existing cmdlets. These improvements are often really cool because it means I can use the old cmdlets in new ways, which is just about like having a new cmdlet.
These add-ons (if you will) are not as easy to find as a whole brand new cmdlet. I mean, a brand new cmdlet is pretty easy to find with Get-Command because all I need to do is type Get-Command, and search for a verb or a noun, or page through all of the cmdlets and look for things that are not familiar. Or, if I want to be more scientific, I can use Get-Command in Windows 8.1, then in Windows 10, and use Compare-Object to compare the two lists of cmdlet names.
Finding new parameters
But finding new parameters is more difficult. It is not impossible, but it is more difficult. In the beta of Windows PowerShell 2.0, for example, one of the Windows PowerShell MVPs wrote a really cool script that would compare drops for new cmdlets and for new parameters. It was a very useful script for keeping up with all the changes that were appearing like rabbits in an unmown field of clover.
In the case of the NoNewLine parameter, it is listed in the What's new in Windows PowerShell 5.0 article, and so that makes it easy to find. At other times, it means paying attention or actually searching by comparing commands from previous versions with commands from the latest version. That can be a rather slow and tedious process.
The problem with new lines
The issue with new lines is not that we now have a solution in search of a problem. It was a problem, and it has been ever since Windows PowerShell 1.0. The reason is that there was inconsistent behavior. For example, if I create a new file and I have text in the file, I will not have a new line. If I add content to a file, I get new lines, and there was no good way to suppress this.
So what is the big deal?
Well, the big deal is that sometimes I want to control the new lines myself—maybe I am trying to build a CSV file or some other file, and I do not want invisible stuff entering my file (such as a carriage return or line feed).
What are the commands that have the new NoNewLine parameter? I can use the Get-Command cmdlet (gcm is an alias) and search for the parameter by using the –ParameterName parameter. Of course, I am searching for the NoNewLine parameter. The command and the associated output are shown in the following image:
The four cmdlets with the new NoNewLine parameter are:
I create a new file and set an initial value. This command and its output are shown here:
PS C:\> New-Item -Path c:\fso\newfile.txt -ItemType file -Value "initial value"
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 8/5/2015 1:33 PM 13 newfile.txt
The file appears in Notepad:
Now I add content to the file, and I do not add a new line. I do this again, and I add two additional lines, but do not use the NoNewLine switched parameter:
PS C:\> Add-Content C:\fso\newfile.txt -Value "more content" -NoNewline
PS C:\> Add-Content C:\fso\newfile.txt -Value "even more content" -NoNewline
PS C:\> Add-Content C:\fso\newfile.txt -Value "additional content"
PS C:\> Add-Content C:\fso\newfile.txt -Value "more additional content"
The file in Notepad looks like this:
Notice that the first time I added “additional content” without adding the NoNewLine parameter, I was adding content to the prior line that did not have a new line, so the “additional content” text appears without a new line. It was only when I added the text the second time without the parameter that “more additional content” appears on its new line.
As shown here, when I use Write-Host with NoNewLine, the output is a bit weird:
Out-File and Set-Content work the same way as adding content to a file because they add directly to a file without adding new lines. So clearly, I have some pretty cool new tools I can use. Sweet.
That is all there is to using the NoNewLine parameter on Windows PowerShell 5.0. Join me tomorrow when I will talk about more cool stuff.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at firstname.lastname@example.org, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy