File Input Output in Powershell (original) (raw)

  1. Home
  2. Programming Projects in Every Language
  3. File Input Output
  4. File Input Output in Powershell

Published on 10 May 2025 (Updated: 10 May 2025)

File Input Output in Powershell

Welcome to the File Input Output in Powershell page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

function Set-File([string]$Filename) {
    try {
        <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>S</mi><mi>t</mi><mi>r</mi><mi>e</mi><mi>a</mi><mi>m</mi><mi>W</mi><mi>r</mi><mi>i</mi><mi>t</mi><mi>e</mi><mi>r</mi><mo>=</mo><mo stretchy="false">[</mo><mi>I</mi><mi>O</mi><mi mathvariant="normal">.</mi><mi>S</mi><mi>t</mi><mi>r</mi><mi>e</mi><mi>a</mi><mi>m</mi><mi>W</mi><mi>r</mi><mi>i</mi><mi>t</mi><mi>e</mi><mi>r</mi><mo stretchy="false">]</mo><mo>:</mo><mo>:</mo><mi>n</mi><mi>e</mi><mi>w</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">StreamWriter = [IO.StreamWriter]::new(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6833em;"></span><span class="mord mathnormal">St</span><span class="mord mathnormal">re</span><span class="mord mathnormal" style="margin-right:0.13889em;">amW</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">i</span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.02778em;">er</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">[</span><span class="mord mathnormal" style="margin-right:0.07847em;">I</span><span class="mord mathnormal" style="margin-right:0.02778em;">O</span><span class="mord">.</span><span class="mord mathnormal">St</span><span class="mord mathnormal">re</span><span class="mord mathnormal" style="margin-right:0.13889em;">amW</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">i</span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.02778em;">er</span><span class="mclose">]</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">::</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">n</span><span class="mord mathnormal">e</span><span class="mord mathnormal" style="margin-right:0.02691em;">w</span><span class="mopen">(</span></span></span></span>FileName)
        $StreamWriter.WriteLine("Hello, PowerShell!")
        $StreamWriter.WriteLine("A line here")
        $StreamWriter.WriteLine("A line there")
        $StreamWriter.WriteLine("Goodbye, PowerShell!")
        $true
    } catch {
        Write-Host "Cannot write to file <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mrow><mi>F</mi><mi>i</mi><mi>l</mi><mi>e</mi><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></mrow><mo>:</mo></mrow><annotation encoding="application/x-tex">{Filename}: </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.13889em;">F</span><span class="mord mathnormal">i</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">e</span><span class="mord mathnormal">nam</span><span class="mord mathnormal">e</span></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">:</span></span></span></span>($_.Exception.Message)"
        $false
    } finally {
        if ($StreamWriter) {
            $StreamWriter.Close()
            $StreamWriter.Dispose()
        }
    }
}

function Get-File([string]$Filename) {
    try {
        foreach ($Line in Get-Content -Path $Filename -ErrorAction Stop) {
            Write-Host $Line
        }
        $true
    } catch {
        Write-Host "Cannot read from file <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mrow><mi>F</mi><mi>i</mi><mi>l</mi><mi>e</mi><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></mrow><mo>:</mo></mrow><annotation encoding="application/x-tex">{Filename}: </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.13889em;">F</span><span class="mord mathnormal">i</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">e</span><span class="mord mathnormal">nam</span><span class="mord mathnormal">e</span></span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">:</span></span></span></span>($_.Exception.Message)"
        $false
    }
}

$Filename = "output.txt"
if (-not (Set-File($Filename))) {
    Exit 1
}

if (-not (Get-File($Filename))) {
    Exit 1
}

File Input Output in Powershell was written by:

If you see anything you'd like to change or update, please consider contributing.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.