Author Posts

December 27, 2016 at 2:12 pm

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?

December 27, 2016 at 3:48 pm

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

December 27, 2016 at 3:55 pm

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

December 27, 2016 at 4:05 pm

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.

December 27, 2016 at 4:25 pm

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

December 27, 2016 at 4:38 pm

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