Some questions

Tagged: 

This topic contains 6 replies, has 3 voices, and was last updated by Profile photo of Tony Antony Tony Antony 3 months ago.

  • Author
    Posts
  • #60855
    Profile photo of Tony Antony
    Tony Antony
    Participant

    Just some questions:

    1. How would I make powershell accept more than one input? For example, let's say I want to add 1+2 so I have $one and $two, but sometimes it could be 1+2+3? How would I do this?

    2. Let's say I have a text file like this:
    zero
    one
    two
    three
    How would I set and save lines one through two [1..2] to be blank so that it's?
    zero
    three

    3. Is there any professional PowerShell schools that I can go to learn more or is books and videos the best method to learn?

  • #60856
    Profile photo of Christian Sandfeld
    Christian Sandfeld
    Participant

    For question 1, one way to do that would be to create a function with a parameter, and make that parameter accept multiple values, and then itterate over that parameter.

    Example:

    function Add-Numbers {
        Param(
            [Parameter(Mandatory = $true)]
            [Int[]]  # < -- The extra [] make the parameter accept an array of input
            $Numbers
        )
        
        foreach ($num in $Numbers)
        {
            $result += $num
        }
        
        Write-Output $result
    }
    
    Add-Numbers -Numbers 1,2,3
    
  • #60858
    Profile photo of Christian Sandfeld
    Christian Sandfeld
    Participant

    For question 2, your sample data could be filtered like this:

    
    $OrgFile = 'C:\Test\test.txt'
    $NewFile = 'C:\Test\test2.txt'
    
    $RemoveText = @(
        'one',
        'two'
    )
    
    Get-Content -Path $OrgFile | Where-Object { $RemoveText -notcontains $_ } | Set-Content -Path $NewFile
    
  • #60859
    Profile photo of Christian Sandfeld
    Christian Sandfeld
    Participant

    And for question 3, I recommend watching the free PowerShell content on Microsoft Virtual Academy. "Getting Started with Microsoft PowerShell" is a good starting point. When it comes to books, the "Learn Windows PowerShell in a Month of Lunches" and "Learn PowerShell Toolmaking in a Month of Lunches" books are good reads. Additionaly Pluralsight have a lot of PowerShell courses as well.

  • #60862
    Profile photo of Olaf Soyk
    Olaf Soyk
    Participant

    Question 3 has been asked several times already here and in other forums like Microsoft Forum for Powershell. You're welcome to use the search function of these forums or even google. 😉

    Look here: Top Beginner Sites and Tutorials

  • #60865
    Profile photo of Olaf Soyk
    Olaf Soyk
    Participant

    For Question 2 the correct answer actually has to be "it depends". A really sinple approach would be something like this:

    $String = Get-Content -Path C:\sample\test.txt
    $String | Select-Object -First 1 -Last 1 | Out-File -FilePath C:\sample\test.txt -Force
    

    But there are thousands of different approaches thinkable depending on the concrete task you need to accomplish

  • #60868
    Profile photo of Tony Antony
    Tony Antony
    Participant

    Thank you

You must be logged in to reply to this topic.