{"id":2962,"date":"2019-09-12T08:36:55","date_gmt":"2019-09-12T15:36:55","guid":{"rendered":"https:\/\/techthoughts.info\/?p=2962"},"modified":"2019-09-12T08:37:01","modified_gmt":"2019-09-12T15:37:01","slug":"powershell-input-output","status":"publish","type":"post","link":"https:\/\/www.techthoughts.info\/powershell-input-output\/","title":{"rendered":"PowerShell Input &#038; Output"},"content":{"rendered":"\n<p>Up to this point in the <a rel=\"noreferrer noopener\" aria-label=\"Learn PowerShell (opens in a new tab)\" href=\"https:\/\/techthoughts.info\/learn-powershell-series\/\" target=\"_blank\">Learn PowerShell<\/a> series we have covered examples using very simple PowerShell input and output. Typically we have statically provided inputs by hard coding a variable (ex. <strong>$input = &#8216;SomeInput&#8217;<\/strong>). Our outputs so far have mostly consisted of <strong>Write-Host<\/strong>. In this lesson we&#8217;ll look to take in more dynamic input and show you how to take more control of your output.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_69_1 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<label for=\"ez-toc-cssicon-toggle-item-69ea680aaac7c\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-69ea680aaac7c\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.techthoughts.info\/powershell-input-output\/#Video\" title=\"Video\">Video<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.techthoughts.info\/powershell-input-output\/#PowerShell_Input\" title=\"PowerShell Input\">PowerShell Input<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.techthoughts.info\/powershell-input-output\/#PowerShell_Input_From_cmdlets\" title=\"PowerShell Input From cmdlets\">PowerShell Input From cmdlets<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.techthoughts.info\/powershell-input-output\/#Read-Host_Input\" title=\"Read-Host Input\">Read-Host Input<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.techthoughts.info\/powershell-input-output\/#Get-Content\" title=\"Get-Content\">Get-Content<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.techthoughts.info\/powershell-input-output\/#PowerShell_Output\" title=\"PowerShell Output\">PowerShell Output<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.techthoughts.info\/powershell-input-output\/#Write-Host\" title=\"Write-Host\">Write-Host<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.techthoughts.info\/powershell-input-output\/#Write-Output\" title=\"Write-Output\">Write-Output<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.techthoughts.info\/powershell-input-output\/#Out-File\" title=\"Out-File\">Out-File<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Video\"><\/span>Video<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p> If you prefer video format over written documentation I discuss this topic in the following TechThoughts video: <\/p>\n\n\n\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<div class=\"video-container\"><iframe loading=\"lazy\" title=\"PowerShell Input &amp; Output\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/nnTlsNA3hPk?feature=oembed&#038;wmode=opaque\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/div>\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"PowerShell_Input\"><\/span>PowerShell Input<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Often times your PowerShell code will need to evaluate some type of input. That input can be statically provided by you ahead of time. There are often use cases though where retrieving a dynamic input is desired.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"PowerShell_Input_From_cmdlets\"><\/span>PowerShell Input From cmdlets<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>You can of course use the output of cmdlets for the input of your PowerShell functionality. Depending on what a cmdlet returns, you could perform a variety of different functions based on that generated input.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>run cmdlets (Get-Process)<ul><li>cmdlet generates output<ul><li>use output as your input<ul><li>perform functionality based on dynamic input<\/li><\/ul><\/li><\/ul><\/li><\/ul><\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">Dynamic PowerShell input from the web<\/h4>\n\n\n\n<p>Lets explore an example where we use PowerShell to pull in dynamic results from a website. If you come from a more Linux focused background this is a bit like using <strong>curl<\/strong>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: Code example:; notranslate\" title=\"Code example:\">\n#retrieve dynamic content from a website\n$webResults = Invoke-WebRequest -Uri &#039;https:\/\/reddit.com\/r\/powershell.json&#039;\n$rawJSON = $webResults.Content\n$objData = $rawJSON | ConvertFrom-Json\n$posts = $objData.data.children.data\n$posts | Select-Object Title,Score | Sort-Object Score -Descending\n<\/pre><\/div>\n\n\n<p>Here we are using <strong><a rel=\"noreferrer noopener\" aria-label=\"Invoke-WebRequest (opens in a new tab)\" href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/microsoft.powershell.utility\/invoke-webrequest?view=powershell-6\" target=\"_blank\">Invoke-WebRequest<\/a><\/strong> to retrieve web content from <a rel=\"noreferrer noopener\" aria-label=\"\/r\/PowerShell (opens in a new tab)\" href=\"https:\/\/www.reddit.com\/r\/PowerShell\/\" target=\"_blank\">\/r\/PowerShell<\/a>. (A great PowerShell resource by the way!) HTML isn&#8217;t particularly useful to us as we want to work with <em>objects<\/em> in PowerShell. So note that I have visited the JSON version of this page. As a result, the output content from this cmdlet is in JSON. Because JSON is structured data, we can take control and convert it to native PowerShell objects using <strong><a rel=\"noreferrer noopener\" aria-label=\"ConvertFrom-JSON (opens in a new tab)\" href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/microsoft.powershell.utility\/convertfrom-json?view=powershell-6\" target=\"_blank\">ConvertFrom-JSON<\/a><\/strong>.<\/p>\n\n\n\n<p><em>Note: There are many of these convert cmdlets. This enables you to take in data from a variety of sources and continue to work with PowerShell objects!<\/em><\/p>\n\n\n\n<p>Once in native object format, it&#8217;s a simple matter to explore the available sub-properties. We can then drill down into them and find post information. Once we have post data, we can select and sort for the inputs we are after. In this example, post title and score (upvotes) are returned.<\/p>\n\n\n\n<p>Run the example above on your machine to see the results. Try exploring other available sub-properties in <strong>$posts<\/strong>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Leveraging cmdlet output as input<\/h4>\n\n\n\n<p>How would you go about using the output from that example as input? <em>That&#8217;s entirely up to you!<\/em> You could write PowerShell code to:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Email yourself the top 3 PowerShell posts of the day<\/li><li>Send yourself a text message with the top post of the day<\/li><li>Display the top 10 posts of the day when you login<\/li><\/ul>\n\n\n\n<p>You&#8217;d be using PowerShell to make yourself smarter on PowerShell! We&#8217;ll be covering more how to create ps1 scripts later in the series so stay tuned!<\/p>\n\n\n\n<figure class=\"wp-block-pullquote\"><blockquote><p> You&#8217;d be using PowerShell to make yourself smarter on PowerShell! <\/p><\/blockquote><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Read-Host_Input\"><\/span>Read-Host Input<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p><strong><a rel=\"noreferrer noopener\" aria-label=\"Read-Host (opens in a new tab)\" href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/microsoft.powershell.utility\/read-host?view=powershell-6\" target=\"_blank\">Read-Host<\/a><\/strong> is another way to get dynamic input from yourself or your users. Leveraging the previous example again, we could only return the desired number of posts:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: Code example:; notranslate\" title=\"Code example:\">\n&#x5B;int]$numPosts = Read-Host -Prompt &quot;Enter the number of posts to read&quot;\n#retrieve dynamic content from a website\n$webResults = Invoke-WebRequest -Uri &#039;https:\/\/reddit.com\/r\/powershell.json&#039;\n$rawJSON = $webResults.Content\n$objData = $rawJSON | ConvertFrom-Json\n$posts = $objData.data.children.data\n$posts | Select-Object Title,url | Sort-Object Score -Descending | Select-Object -First $numPosts\n<\/pre><\/div>\n\n\n<p>The -First parameter of <strong>Select-Object<\/strong> allows us to control how many objects we return to the console. Because we are sorting by number of upvotes, we will return the number of top posts specified. Run this on your computer specifying different numbers of posts.<\/p>\n\n\n\n<p>Here is fun example where we can retrieve web data combined with <strong>Read-Host<\/strong> to display the desired number of cat facts:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: Code example:; notranslate\" title=\"Code example:\">\n&#x5B;int]$numFacts = Read-Host -Prompt &quot;Enter the number of cat facts you would like&quot;\n$webResults = Invoke-RestMethod -Uri &quot;https:\/\/catfact.ninja\/facts?limit=$numFacts&amp;max_length=140&quot;\n$webResults.data\n<\/pre><\/div>\n\n\n<p><em>Note that catfact.ninja is an <strong>API <\/strong>and not a traditional webpage. As a result, <\/em><strong><em><a href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/microsoft.powershell.utility\/invoke-restmethod?view=powershell-6\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"Invoke-RestMethod (opens in a new tab)\">Invoke-RestMethod<\/a><\/em><\/strong><em> is the appropriate cmdlet, and not <\/em><strong><em>Invoke-WebRequest<\/em><\/strong><em>.<\/em><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Get-Content\"><\/span>Get-Content<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Another source of dynamic input that you often need comes from files on a device. Operationally, you&#8217;ll often be looking at log files. Lets take a look at an example log file:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: Code example:; notranslate\" title=\"Code example:\">\n&#x5B;INFO]&#x5B;Date]Normal Operations\n&#x5B;INFO]&#x5B;Date]Normal Operations\n&#x5B;INFO]&#x5B;Date]Normal Operations\n&#x5B;ERROR]&#x5B;Date]192.168.1.5 unable to access\n&#x5B;INFO]&#x5B;Date]Normal Operations\n<\/pre><\/div>\n\n\n<p><a rel=\"noreferrer noopener\" aria-label=\"Get-Content (opens in a new tab)\" href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/microsoft.powershell.management\/get-content?view=powershell-6\" target=\"_blank\"><strong>Get-Content<\/strong><\/a> can retrieve the contents of a file for you to use as input.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: Code example:; notranslate\" title=\"Code example:\">\n$logContent = Get-Content C:\\Test\\SampleLog.log\n\n# get just entries that have an IP address using regex\n$regex = &quot;\\b\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\b&quot;\n$logContent | Select-String -Pattern $regex -AllMatches\n\n# get just entries that have an IP address using Where-Object\n$logContent | Where-Object {$_ -like &quot;*.*.*.*&quot;}\n<\/pre><\/div>\n\n\n<p>The contents of the SampleLog are stored in <strong>$logContent<\/strong>. Now that you have that input data you can start doing evaluations. It may be critical that PowerShell take some form of action if an IP address is discovered in the log. You can parse the data using <a rel=\"noreferrer noopener\" aria-label=\"regular expression (opens in a new tab)\" href=\"https:\/\/en.wikipedia.org\/wiki\/Regular_expression\" target=\"_blank\">regular expression<\/a> (regex) or by using Where-Object. If an IP is discovered, you could ticket, pop a notification, or perform some other action.<\/p>\n\n\n\n<p>You aren&#8217;t limited to just text or standard log files. Get-Content can pull in input data from a wide array of file types. Here is an example where you can get input from a csv file. Remember that there are many conversion cmdlets so you can get csv data into normal PowerShell object format easily!<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: Code example:; notranslate\" title=\"Code example:\">\n$rawCSVInput = Get-Content C:\\Test\\SampleCSVFile.csv\n$objData = $rawCSVInput | ConvertFrom-Csv\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"PowerShell_Output\"><\/span>PowerShell Output<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>If you want to display results back to the console or generate a results file, you&#8217;ll need to generate some type of PowerShell output. There are a few ways to accomplish this, and one you should avoid!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Write-Host\"><\/span>Write-Host<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Write-Host can output customized strings to the console. You can customize the output by controlling the start of a new line, or -NoNewline. A foreground and background color can also be set. Try the following examples:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: Code example:; notranslate\" title=\"Code example:\">\n# Output simple string to console\nWrite-Host &#039;Text output to console&#039;\n\n# Customize output to the console with colors:\nWrite-Host &quot;Warning&quot; -ForegroundColor Yellow\nWrite-Host &quot;ERROR&quot; -ForegroundColor Red\nWrite-Host &quot;Works Great&quot; -ForegroundColor Green\nWrite-Host &quot;CRITICAL ERROR&quot; -BackgroundColor Red -ForegroundColor White\n<\/pre><\/div>\n\n\n<p>Write-Host is only capable of string output, so you need to make sure you are giving it a string. If not, Write-Host will do its best to accommodate some type of output, but the results may not be what you expect.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: Code example:; notranslate\" title=\"Code example:\">\n$hostInfo = Get-Host\nWrite-Host $hostInfo\n<\/pre><\/div>\n\n\n<p>In the above example, you&#8217;ll get the output of: <strong>System.Management.Automation.Internal.Host.InternalHost<\/strong><\/p>\n\n\n\n<p>This is because <strong>$hostInfo<\/strong> is a PowerShell object, not a string. We can drill down farther into this object until we have a string that is use-able by Write-Host:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: Code example:; notranslate\" title=\"Code example:\">\n$hostInfo = Get-Host\nWrite-Host $hostInfo\nWrite-Host $hostInfo.Version\n<\/pre><\/div>\n\n\n<p>Write-Host is easy to implement, and it&#8217;s color capabilities make it an attractive choice for displaying results to users. However, you shouldn&#8217;t use it. It can be handy to use when writing new PowerShell to verify your code contains values you expect. Beyond that, its use should be avoided. Jeffrey Snover covers the reasons why in his post <a rel=\"noreferrer noopener\" aria-label=\"Write-Host Considered Harmful (opens in a new tab)\" href=\"https:\/\/www.jsnover.com\/blog\/2013\/12\/07\/write-host-considered-harmful\/\" target=\"_blank\"><em>Write-Host Considered Harmful<\/em><\/a>. In it he advocates for the use of Write-Verbose over Write-Host. We&#8217;ll be covering Write-Verbose later in the series when we begin creating PowerShell functions.<\/p>\n\n\n\n<figure class=\"wp-block-pullquote\"><blockquote><p>Using Write-Host is almost always wrong.<\/p><cite>Jeffrey Snover &#8211; creator of PowerShell<\/cite><\/blockquote><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Write-Output\"><\/span>Write-Output<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>If you&#8217;ve been following along in the series you&#8217;ve already used <a rel=\"noreferrer noopener\" aria-label=\"Write-Output (opens in a new tab)\" href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/microsoft.powershell.utility\/write-output?view=powershell-6\" target=\"_blank\"><strong>Write-Output<\/strong><\/a> quite a bit. This is because Write-Output is what PowerShell is using behind the scenes as the default output stream.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: Code example:; notranslate\" title=\"Code example:\">\nGet-Process\n<\/pre><\/div>\n\n\n<p>When you run <strong>Get-Process<\/strong> and see results in the PowerShell console, that was accomplished by Write-Output. All of these examples accomplish the same thing:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: Code example:; notranslate\" title=\"Code example:\">\n# Example 1\nGet-Process\n\n# Example 2\n$processes = Get-Process\nWrite-Output $processes\n\n# Example 3\n$processes = Get-Process\n$processes\n<\/pre><\/div>\n\n\n<p>As a result, you won&#8217;t often see Write-Output written in PowerShell code. It&#8217;s implied as the default output behavior.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Out-File\"><\/span>Out-File<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>The console isn&#8217;t your only avenue for PowerShell output. For times when you want to output to a file, you can leverage <strong><a rel=\"noreferrer noopener\" aria-label=\"Out-File (opens in a new tab)\" href=\"https:\/\/docs.microsoft.com\/en-us\/powershell\/module\/microsoft.powershell.utility\/out-file?view=powershell-6\" target=\"_blank\">Out-File<\/a><\/strong>. <strong>Out-File<\/strong> is capable of sending the entire output to file. This means that it&#8217;s a bit more intelligent than <strong>Write-Host<\/strong>, and can handle sending the entire object return from a cmdlet to file. See below for some examples:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: Code example:; notranslate\" title=\"Code example:\">\n# Example 1\n$processes = Get-Process\n$processes | Out-File -FilePath C:\\Test\\processInfo.txt\n\n# Example 2\nGet-Process | Out-File -FilePath C:\\Test\\processInfo.txt\n<\/pre><\/div>\n\n\n<p>Don&#8217;t forget about those conversion cmdlets! There are several for outputs as well! So, if your file needs to be in CSV format, you can leverage <strong>ConvertTo-CSV<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: Code example:; notranslate\" title=\"Code example:\">\n$processes = Get-Process\n$processes | ConvertTo-Csv -NoTypeInformation | Out-File c:\\test\\processes.csv\n<\/pre><\/div>\n\n\n<p>There are a few parameters you&#8217;ll want to familiarize yourself with for <strong>Out-File<\/strong>.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>NoClobber &#8211; prevents an existing file from being overwritten<\/li><li>Append  &#8211; instead of overwriting an output file, add to it<\/li><li>Encoding &#8211; especially useful if you are running on Linux &#8211; specify the encoding type of the file<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Up to this point in the Learn PowerShell series we have covered examples using very simple PowerShell input and output. Typically we have statically provided inputs by hard coding a variable (ex. $input =&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":3013,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[421],"tags":[265],"series":[436],"class_list":["post-2962","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development","tag-powershell","series-learn-powershell"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>PowerShell Input &amp; Output - Tech Thoughts<\/title>\n<meta name=\"description\" content=\"Learn how to take in more dynamic PowerShell input and generate various types of PowerShell output. See how to take input from the web and convert output.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.techthoughts.info\/powershell-input-output\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PowerShell Input &amp; Output - Tech Thoughts\" \/>\n<meta property=\"og:description\" content=\"Learn how to take in more dynamic PowerShell input and generate various types of PowerShell output. See how to take input from the web and convert output.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.techthoughts.info\/powershell-input-output\/\" \/>\n<meta property=\"og:site_name\" content=\"Tech Thoughts\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/techthoughts2\" \/>\n<meta property=\"article:published_time\" content=\"2019-09-12T15:36:55+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-09-12T15:37:01+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.techthoughts.info\/wp-content\/uploads\/2019\/09\/powershell_input_output.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1280\" \/>\n\t<meta property=\"og:image:height\" content=\"720\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Jake\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@JakeMorrison\" \/>\n<meta name=\"twitter:site\" content=\"@JakeMorrison\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jake\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.techthoughts.info\/powershell-input-output\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.techthoughts.info\/powershell-input-output\/\"},\"author\":{\"name\":\"Jake\",\"@id\":\"https:\/\/www.techthoughts.info\/#\/schema\/person\/422a290e8bc3f6a94775f6290c26105d\"},\"headline\":\"PowerShell Input &#038; Output\",\"datePublished\":\"2019-09-12T15:36:55+00:00\",\"dateModified\":\"2019-09-12T15:37:01+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.techthoughts.info\/powershell-input-output\/\"},\"wordCount\":1278,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.techthoughts.info\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.techthoughts.info\/powershell-input-output\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.techthoughts.info\/wp-content\/uploads\/2019\/09\/powershell_input_output.jpg\",\"keywords\":[\"PowerShell\"],\"articleSection\":[\"Development\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.techthoughts.info\/powershell-input-output\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.techthoughts.info\/powershell-input-output\/\",\"url\":\"https:\/\/www.techthoughts.info\/powershell-input-output\/\",\"name\":\"PowerShell Input & Output - Tech Thoughts\",\"isPartOf\":{\"@id\":\"https:\/\/www.techthoughts.info\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.techthoughts.info\/powershell-input-output\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.techthoughts.info\/powershell-input-output\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.techthoughts.info\/wp-content\/uploads\/2019\/09\/powershell_input_output.jpg\",\"datePublished\":\"2019-09-12T15:36:55+00:00\",\"dateModified\":\"2019-09-12T15:37:01+00:00\",\"description\":\"Learn how to take in more dynamic PowerShell input and generate various types of PowerShell output. See how to take input from the web and convert output.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.techthoughts.info\/powershell-input-output\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.techthoughts.info\/powershell-input-output\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.techthoughts.info\/powershell-input-output\/#primaryimage\",\"url\":\"https:\/\/www.techthoughts.info\/wp-content\/uploads\/2019\/09\/powershell_input_output.jpg\",\"contentUrl\":\"https:\/\/www.techthoughts.info\/wp-content\/uploads\/2019\/09\/powershell_input_output.jpg\",\"width\":1280,\"height\":720,\"caption\":\"Learn PowerShell Episode 7 - PowerShell Input & Output\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.techthoughts.info\/powershell-input-output\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Tech Thoughts\",\"item\":\"https:\/\/www.techthoughts.info\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PowerShell Input &#038; Output\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.techthoughts.info\/#website\",\"url\":\"https:\/\/www.techthoughts.info\/\",\"name\":\"Tech Thoughts\",\"description\":\"A cloud, PowerShell, server, hardware, technology, and more tech thoughts blog\",\"publisher\":{\"@id\":\"https:\/\/www.techthoughts.info\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.techthoughts.info\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.techthoughts.info\/#organization\",\"name\":\"TechThoughts\",\"url\":\"https:\/\/www.techthoughts.info\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.techthoughts.info\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.techthoughts.info\/wp-content\/uploads\/2017\/12\/techthoughts_icon.png\",\"contentUrl\":\"https:\/\/www.techthoughts.info\/wp-content\/uploads\/2017\/12\/techthoughts_icon.png\",\"width\":110,\"height\":109,\"caption\":\"TechThoughts\"},\"image\":{\"@id\":\"https:\/\/www.techthoughts.info\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/techthoughts2\",\"https:\/\/x.com\/JakeMorrison\",\"https:\/\/www.youtube.com\/TechthoughtsInfo2\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.techthoughts.info\/#\/schema\/person\/422a290e8bc3f6a94775f6290c26105d\",\"name\":\"Jake\",\"description\":\"A little about me...\",\"sameAs\":[\"https:\/\/jakewmorrison.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PowerShell Input & Output - Tech Thoughts","description":"Learn how to take in more dynamic PowerShell input and generate various types of PowerShell output. See how to take input from the web and convert output.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.techthoughts.info\/powershell-input-output\/","og_locale":"en_US","og_type":"article","og_title":"PowerShell Input & Output - Tech Thoughts","og_description":"Learn how to take in more dynamic PowerShell input and generate various types of PowerShell output. See how to take input from the web and convert output.","og_url":"https:\/\/www.techthoughts.info\/powershell-input-output\/","og_site_name":"Tech Thoughts","article_publisher":"https:\/\/www.facebook.com\/techthoughts2","article_published_time":"2019-09-12T15:36:55+00:00","article_modified_time":"2019-09-12T15:37:01+00:00","og_image":[{"width":1280,"height":720,"url":"https:\/\/www.techthoughts.info\/wp-content\/uploads\/2019\/09\/powershell_input_output.jpg","type":"image\/jpeg"}],"author":"Jake","twitter_card":"summary_large_image","twitter_creator":"@JakeMorrison","twitter_site":"@JakeMorrison","twitter_misc":{"Written by":"Jake","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.techthoughts.info\/powershell-input-output\/#article","isPartOf":{"@id":"https:\/\/www.techthoughts.info\/powershell-input-output\/"},"author":{"name":"Jake","@id":"https:\/\/www.techthoughts.info\/#\/schema\/person\/422a290e8bc3f6a94775f6290c26105d"},"headline":"PowerShell Input &#038; Output","datePublished":"2019-09-12T15:36:55+00:00","dateModified":"2019-09-12T15:37:01+00:00","mainEntityOfPage":{"@id":"https:\/\/www.techthoughts.info\/powershell-input-output\/"},"wordCount":1278,"commentCount":0,"publisher":{"@id":"https:\/\/www.techthoughts.info\/#organization"},"image":{"@id":"https:\/\/www.techthoughts.info\/powershell-input-output\/#primaryimage"},"thumbnailUrl":"https:\/\/www.techthoughts.info\/wp-content\/uploads\/2019\/09\/powershell_input_output.jpg","keywords":["PowerShell"],"articleSection":["Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.techthoughts.info\/powershell-input-output\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.techthoughts.info\/powershell-input-output\/","url":"https:\/\/www.techthoughts.info\/powershell-input-output\/","name":"PowerShell Input & Output - Tech Thoughts","isPartOf":{"@id":"https:\/\/www.techthoughts.info\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.techthoughts.info\/powershell-input-output\/#primaryimage"},"image":{"@id":"https:\/\/www.techthoughts.info\/powershell-input-output\/#primaryimage"},"thumbnailUrl":"https:\/\/www.techthoughts.info\/wp-content\/uploads\/2019\/09\/powershell_input_output.jpg","datePublished":"2019-09-12T15:36:55+00:00","dateModified":"2019-09-12T15:37:01+00:00","description":"Learn how to take in more dynamic PowerShell input and generate various types of PowerShell output. See how to take input from the web and convert output.","breadcrumb":{"@id":"https:\/\/www.techthoughts.info\/powershell-input-output\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.techthoughts.info\/powershell-input-output\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.techthoughts.info\/powershell-input-output\/#primaryimage","url":"https:\/\/www.techthoughts.info\/wp-content\/uploads\/2019\/09\/powershell_input_output.jpg","contentUrl":"https:\/\/www.techthoughts.info\/wp-content\/uploads\/2019\/09\/powershell_input_output.jpg","width":1280,"height":720,"caption":"Learn PowerShell Episode 7 - PowerShell Input & Output"},{"@type":"BreadcrumbList","@id":"https:\/\/www.techthoughts.info\/powershell-input-output\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Tech Thoughts","item":"https:\/\/www.techthoughts.info\/"},{"@type":"ListItem","position":2,"name":"PowerShell Input &#038; Output"}]},{"@type":"WebSite","@id":"https:\/\/www.techthoughts.info\/#website","url":"https:\/\/www.techthoughts.info\/","name":"Tech Thoughts","description":"A cloud, PowerShell, server, hardware, technology, and more tech thoughts blog","publisher":{"@id":"https:\/\/www.techthoughts.info\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.techthoughts.info\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.techthoughts.info\/#organization","name":"TechThoughts","url":"https:\/\/www.techthoughts.info\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.techthoughts.info\/#\/schema\/logo\/image\/","url":"https:\/\/www.techthoughts.info\/wp-content\/uploads\/2017\/12\/techthoughts_icon.png","contentUrl":"https:\/\/www.techthoughts.info\/wp-content\/uploads\/2017\/12\/techthoughts_icon.png","width":110,"height":109,"caption":"TechThoughts"},"image":{"@id":"https:\/\/www.techthoughts.info\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/techthoughts2","https:\/\/x.com\/JakeMorrison","https:\/\/www.youtube.com\/TechthoughtsInfo2"]},{"@type":"Person","@id":"https:\/\/www.techthoughts.info\/#\/schema\/person\/422a290e8bc3f6a94775f6290c26105d","name":"Jake","description":"A little about me...","sameAs":["https:\/\/jakewmorrison.com"]}]}},"jetpack_featured_media_url":"https:\/\/www.techthoughts.info\/wp-content\/uploads\/2019\/09\/powershell_input_output.jpg","jetpack_shortlink":"https:\/\/wp.me\/p2EFB0-LM","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techthoughts.info\/wp-json\/wp\/v2\/posts\/2962"}],"collection":[{"href":"https:\/\/www.techthoughts.info\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techthoughts.info\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techthoughts.info\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techthoughts.info\/wp-json\/wp\/v2\/comments?post=2962"}],"version-history":[{"count":35,"href":"https:\/\/www.techthoughts.info\/wp-json\/wp\/v2\/posts\/2962\/revisions"}],"predecessor-version":[{"id":3012,"href":"https:\/\/www.techthoughts.info\/wp-json\/wp\/v2\/posts\/2962\/revisions\/3012"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techthoughts.info\/wp-json\/wp\/v2\/media\/3013"}],"wp:attachment":[{"href":"https:\/\/www.techthoughts.info\/wp-json\/wp\/v2\/media?parent=2962"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techthoughts.info\/wp-json\/wp\/v2\/categories?post=2962"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techthoughts.info\/wp-json\/wp\/v2\/tags?post=2962"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/www.techthoughts.info\/wp-json\/wp\/v2\/series?post=2962"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}