{"id":74121,"date":"2015-08-07T00:01:00","date_gmt":"2015-08-07T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/08\/07\/the-powershell-5-nonewline-parameter\/"},"modified":"2019-02-18T09:46:42","modified_gmt":"2019-02-18T16:46:42","slug":"the-powershell-5-nonewline-parameter","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/the-powershell-5-nonewline-parameter\/","title":{"rendered":"The PowerShell 5 NoNewLine Parameter"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Ed Wilson, Microsoft Scripting Guy, talks about the NoNewLine parameter in Windows PowerShell 5.0 in Windows 10.<\/span><\/p>\n<p>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.<\/p>\n<p>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 <b>Get-Command<\/b> because all I need to do is type <b>Get-Command<\/b>, 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 <b>Get-Command<\/b> in Windows 8.1, then in Windows 10, and use <b>Compare-Object<\/b> to compare the two lists of cmdlet names.<\/p>\n<h2>Finding new parameters<\/h2>\n<p>But finding new parameters is more difficult. It is not impossible, but it is more difficult. In the beta of Windows PowerShell&nbsp;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.<\/p>\n<p>In the case of the <b>NoNewLine<\/b> parameter, it is listed in the <a href=\"https:\/\/technet.microsoft.com\/en-us\/library\/hh857339.aspx#BKMK_new50\" target=\"_blank\">What&#039;s new in Windows PowerShell 5.0<\/a> 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.<\/p>\n<h2>The problem with new lines<\/h2>\n<p>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&nbsp;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.<\/p>\n<p>So what is the big deal?<\/p>\n<p>Well, the big deal is that sometimes I want to control the new lines myself&mdash;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).<\/p>\n<p>What are the commands that have the new <b>NoNewLine<\/b> parameter? I can use the <b>Get-Command<\/b> cmdlet (<b>gcm<\/b> is an alias) and search for the parameter by using the <b>&ndash;ParameterName<\/b> parameter. Of course, I am searching for the <b>NoNewLine<\/b> parameter. The command and the associated output are shown in the following image:<\/p>\n<p style=\"margin-left:30px\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-8-7-15-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-8-7-15-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>The four cmdlets with the new <b>NoNewLine<\/b> parameter are:<\/p>\n<ul>\n<li>Add-Content<\/li>\n<li>Out-File<\/li>\n<li>Set-Content<\/li>\n<li>Write-Host.<\/li>\n<\/ul>\n<p>I create a new file and set an initial value. This command and its output are shown here:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; New-Item -Path c:\\fso\\newfile.txt -ItemType file -Value &quot;initial value&quot;<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; Directory: C:\\fso<\/p>\n<p style=\"margin-left:30px\">Mode&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LastWriteTime&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Length Name<\/p>\n<p style=\"margin-left:30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212; &#8212;-<\/p>\n<p style=\"margin-left:30px\">-a&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 8\/5\/2015&nbsp;&nbsp; 1:33 PM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 13 newfile.txt<\/p>\n<p>The file appears in Notepad:<\/p>\n<p style=\"margin-left:30px\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-8-7-15-03.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-8-7-15-03.png\" alt=\"Image of text\" title=\"Image of text\" \/><\/a><\/p>\n<p>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 <b>NoNewLine<\/b> switched parameter:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; Add-Content C:\\fso\\newfile.txt -Value &quot;more content&quot; -NoNewline<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; Add-Content C:\\fso\\newfile.txt -Value &quot;even more content&quot; -NoNewline<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; Add-Content C:\\fso\\newfile.txt -Value &quot;additional content&quot;<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; Add-Content C:\\fso\\newfile.txt -Value &quot;more additional content&quot;<\/p>\n<p>The file in Notepad looks like this:<\/p>\n<p style=\"margin-left:30px\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5633.hsg-8-7-15-04.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5633.hsg-8-7-15-04.png\" alt=\"Image of text\" title=\"Image of text\" \/><\/a><\/p>\n<p>Notice that the first time I added &ldquo;additional content&rdquo; without adding the <b>NoNewLine<\/b> parameter, I was adding content to the prior line that did not have a new line, so the &ldquo;additional content&rdquo; text appears without a new line. It was only when I added the text the second time without the parameter that &ldquo;more additional content&rdquo; appears on its new line.<\/p>\n<p>As shown here, when I use <b>Write-Host<\/b> with <b>NoNewLine<\/b>, the output is a bit weird:<\/p>\n<p style=\"margin-left:30px\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-8-7-15-05.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-8-7-15-05.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p><b>Out-File<\/b> and <b>Set-Content<\/b> 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.<\/p>\n<p>That is all there is to using the <b>NoNewLine<\/b> parameter on Windows PowerShell 5.0. Join me tomorrow when I will talk about more cool stuff.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[51,609,3,608,45],"class_list":["post-74121","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-powershell-5","tag-scripting-guy","tag-windows-10","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/74121","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=74121"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/74121\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=74121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=74121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=74121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}