none
Looking for help with a few powershell scripts

    Question

  • I have a few questions concerning powershell scripts, that I hope someone can help with.

    First is how I can make a script that asks for my birthdate, and then calculates how long I have lived, from this information. I have got the asking part I think, but I don't know how to get the script to calculate from it?

    A second script I am trying to make, is a menu where you can choose between 3 different scripts that will run, when I enter a number from 1 to 3. But how do I associate these scripts with the 3 choices? Is this even possible in powershell?

    All help will be appreciated!

    Wednesday, November 7, 2018 11:06 PM

All replies

  • You may have missed the first post at the top of this forum:

    This forum is for scripting questions rather than script requests

    What have you tried so far, and with what results?


    -- Bill Stewart [Bill_Stewart]

    Wednesday, November 7, 2018 11:10 PM
    Moderator
  • I can only give some pointers.

    For first request read about Get-Date. Take two dates (now and birthday) and subtract one from the other.

    The second request I assume you mean making general command line interfaces. You already know how to read-host, now you only need to know how to use switch. Read about it by using Get-Help about_Switch.

    Thursday, November 8, 2018 11:43 AM
  • Sorry if it is the wrong forum, but I do have some code, so thought I would try it here.

    For the menu I was talking about I have the following:

    do
    {
         Show-Menu
         $input = Read-Host "Please make a selection"
         switch ($input)
         {
               '1' {
                    cls
                    'You chose option #1'
               } '2' {
                    cls
                    'You chose option #2'
               } '3' {
                    cls
                    'You chose option #3'
               } 'q' {
                    return
               }
         }
         pause
    }
    until ($input -eq 'q')

    But how do I associate options 1-3 with other scripts, so it runs them when selected?

    To calculate ones bitthday I have tried this with no luck:

    Write-host "Hvad er din fødselsdato?"
    $Readhost = Read-Host
    $now = [datetime]::now
    [datetime]$birthday
    $age = [datetime]$now - $birthday
    Write-Host "din alder er:" $age.Days "days" ($age.Hours) "hours" ($age.Minutes) "minutes" ($age.Milliseconds) "seconds"

    Thursday, November 8, 2018 4:11 PM
  • You can run other files inside PowerShell by doing

    & "C:\PathToFile\File.ps1"


    Your birthday example has the right idea but needs fixing. You can avoid most of the mistakes if you look for lessons online for PowerShell.

    Here you are saving to $Readhost, but you never used it again in the script. Maybe you meant to use $birthday in this line instead. Also the Read-Host cmdlet has a parameter that displays a message which you can use to avoid using Write-Host in the previous line.

    $Readhost = Read-Host

    This line you have the right idea of changing the string to a datetime, but it will only change $birthday temporarily because you aren't assigning (saving) your results by using =, and because there are no = it will send the reulst to your screen instead. Then again you don't really need to assign in this line, in fact you can remove this line entirely and apply [datetime] to the next $birthday instead.

    [datetime]$birthday 

    This line has two issues, you are trying to cast $now to a datetime even though it already is a datetime, and you are trying to subtract by $birthday which is still a string type (what you originally did in the previous line does not change birthday's type permanently).

    $age = [datetime]$now - $birthday
    Monday, November 12, 2018 1:25 PM