PowerShell Scripting - The Basics  

Page 2 of 4 FirstFirst 1234 LastLast

  1. Posts : 5,833
    Dual boot Windows 10 FCU Pro x 64 & current Insider 10 Pro
       #10

    Great tutorial, Kari.

    Running 10 Pro FCU and Insider 17017. Using PS ISE Admin.

    First things first > Before I get started with learning scripting basics, I was going through the interface to get familiar with it. I see it doesn't have "About" in the help menu. When I click on Help/F1 it takes me to:

    Windows PowerShell Integrated Scripting Environment ISE | Microsoft Docs

    It shows version 5.1 at the left. There's a v6 shown in that drop down menu. In my Windows/System32/WindowsPowerShell/v1.0 the .exe shows as version 1 in properties. Is there a way to get v6?

    I have a PS ISE tile on my Start screen. Is there a way to start the program with Admin rights without going through the UAC prompt? I have to right click the tile and go through its context menu to start in that mode. First-world problem I know.
      My Computers


  2. Posts : 15,480
    Windows10
       #11

    Kari said:
    PowerShell has no issues with logical operators. In screenshot beginning of a sample script reading 3 numbers and outputting them in ascending order, comparison in IF statement uses one AND operator (-and in PS) and in ELSEIF statement two AND operators:
    Attachment 159748

    You can use AND (-and), OR (-or), XOR (-xor) and NOT (-not).




    Yes it is.

    Code:
    if (this condition is met)
         {
         Do this
         Then do this
         Finally do this, too
         }

    An IF statement can also include other IF statements:

    Code:
    if (this condition is met)
         {
         if (this condition is met, too)
              {
              Do this
              Then do this
              Finally do this, too
              }
         Do this
         Do that
         }

    Kari
    Thanks very much. I shall have to experiment now .
      My Computer


  3. Posts : 15,480
    Windows10
       #12

    slicendice said:
    @cereberus

    I am sorry, I must have used only half my brain this morning and read your question only partially. I did not answer your question at all. Instead I got stuck at your comment about the funny looking operators.

    The correct answer to your question is exactly what @Kari said.
    No problem - we all have brain fart days.
      My Computer


  4. Posts : 17,661
    Windows 10 Pro
    Thread Starter
       #13

    HippsieGypsie said:
    Is there a way to get v6?
    PowerShell Core, still in beta, is a relatively new version of PowerShell, difference to original native Windows PowerShell being it's open source and can be installed and used in Windows, Linux and Mac. PowerShell Core is currently in version 6.0.0-beta.8: Releases · PowerShell/PowerShell · GitHub

    Although (almost fully) compatible with Windows native PS, I recommend staying in native PS until the time comes that both versions merge to one. Current version of native Windows PS is 5.1:
    PowerShell Scripting - The Basics-image.png


    HippsieGypsie said:
    I have a PS ISE tile on my Start screen. Is there a way to start the program with Admin rights without going through the UAC prompt? I have to right click the tile and go through its context menu to start in that mode. First-world problem I know.
    Create an elevated shortcut for it: Create Elevated Shortcut without UAC prompt in Windows 10 Customization Tutorials

    Kari
      My Computer


  5. Posts : 17,661
    Windows 10 Pro
    Thread Starter
       #14

    cereberus said:
    Thanks very much. I shall have to experiment now .
    Just remember to use parentheses and curly brackets correctly.
    IF (this) {DO that}
    IF ((this) -or (this)) {DO that}
    IF ((this) -and (this)) {DO that}

    A "real life" example of using OR (-or), from second tut in PS scripting (will be published in couple of hours) about using a PS script to create bootable USB Windows 10 install media, I'll use a conditional statement as shown below in CODE box to check if the folder / mounted ISO given by user contains Windows setup files by checking if install.wim or install.esd exists either in \Sources folder (single bit architecture ISO) or at least in one of either \x86\Sources and x64\Sources folders (dual architecture ISO).

    Script has read user input just before storing it, either drive letter for a mounted W10 ISO or path to a folder containing files copied from ISO, in variable $ISOFolder. Using cmdlet Test-Path to check if a file exists, if true the condition is met:

    Code:
    $WimCount = 0
    if ((Test-Path $ISOFolder\Sources\install.wim) -or 
        (Test-Path $ISOFolder\x86\Sources\install.wim) -or
        (Test-Path $ISOFolder\x64\Sources\install.wim) -or
        (Test-Path $ISOFolder\Sources\install.esd) -or 
        (Test-Path $ISOFolder\x86\Sources\install.esd) -or
        (Test-Path $ISOFolder\x64\Sources\install.esd))
            {$WimCount = 1}        
        else 
            {
            cls
            Write-Host
            Write-Host ' No Windows 10 installation files found.'
            Write-Host ' Please check mounted ISO letter or path'
            Write-Host ' to folder containing installation files'
            Write-Host ' and run script again.'
            Write-Host
            Pause
            Exit
            }

    Using five OR operators to check if any of six conditions is met is not very elegant but it does the job :)

    Kari
      My Computer


  6. Posts : 17,661
    Windows 10 Pro
    Thread Starter
       #15

    When the scripting basics is clear, see the next part in PS scripting tutorials: PowerShell Scripting - Create USB Install Media for Windows 10 Installation Upgrade Tutorials
      My Computer


  7. Posts : 33
    Windows 10 Pro for Workstations
       #16

    The proper way to do any If statements is by using Try/Catch/Finally blocks.
      My Computers


  8. Posts : 4,666
    Windows 10 Pro x64 21H1 Build 19043.1151 (Branch: Release Preview)
       #17

    DrEmpiricism said:
    The proper way to do any If statements is by using Try/Catch/Finally blocks.
    Hahahah! Are you serious?
      My Computers


  9. Posts : 15,480
    Windows10
       #18

    Kari said:
    Just remember to use parentheses and curly brackets correctly.
    IF (this) {DO that}
    IF ((this) -or (this)) {DO that}
    IF ((this) -and (this)) {DO that}

    A "real life" example of using OR (-or), from second tut in PS scripting (will be published in couple of hours) about using a PS script to create bootable USB Windows 10 install media, I'll use a conditional statement as shown below in CODE box to check if the folder / mounted ISO given by user contains Windows setup files by checking if install.wim or install.esd exists either in \Sources folder (single bit architecture ISO) or at least in one of either \x86\Sources and x64\Sources folders (dual architecture ISO).

    Script has read user input just before storing it, either drive letter for a mounted W10 ISO or path to a folder containing files copied from ISO, in variable $ISOFolder. Using cmdlet Test-Path to check if a file exists, if true the condition is met:

    Code:
    $WimCount = 0
    if ((Test-Path $ISOFolder\Sources\install.wim) -or 
        (Test-Path $ISOFolder\x86\Sources\install.wim) -or
        (Test-Path $ISOFolder\x64\Sources\install.wim) -or
        (Test-Path $ISOFolder\Sources\install.esd) -or 
        (Test-Path $ISOFolder\x86\Sources\install.esd) -or
        (Test-Path $ISOFolder\x64\Sources\install.esd))
            {$WimCount = 1}        
        else 
            {
            cls
            Write-Host
            Write-Host ' No Windows 10 installation files found.'
            Write-Host ' Please check mounted ISO letter or path'
            Write-Host ' to folder containing installation files'
            Write-Host ' and run script again.'
            Write-Host
            Pause
            Exit
            }

    Using five OR operators to check if any of six conditions is met is not very elegant but it does the job :)

    Kari
    Actually, I think it is very elegant and clear.
      My Computer


  10. Posts : 17,661
    Windows 10 Pro
    Thread Starter
       #19

    DrEmpiricism said:
    The proper way to do any If statements is by using Try/Catch/Finally blocks.
    This is the first part of series of PowerShell scripting tutorials I intend to write in coming months, emphasis in words first part. The idea of this tutorial is to introduce concept of PS scripting to those not familiar with it. Error handling, "catch" in coding principle try - catch - finally you refer to does most certainly not belong to it.

    In similar way this tutorial tells nothing about do while loops or functions or calling VBS from PS script or many other features and finesses of PS scripting. Your comment is like asking why math textbook for grade 5 does not mention Riemannian geometry.


    cereberus said:
    Actually, I think it is very elegant and clear.
    That conditional statement is now "live" in the next part of PS scripting tuts: PowerShell Scripting - Create USB Install Media for Windows 10 Installation Upgrade Tutorials

    Kari
      My Computer


 

Tutorial Categories

PowerShell Scripting - The Basics Tutorial Index Network & Sharing Instalation and Upgrade Browsers and Email General Tips Gaming Customization Apps and Features Virtualization BSOD System Security User Accounts Hardware and Drivers Updates and Activation Backup and Restore Performance and Maintenance Mixed Reality Phone


  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 01:26.
Find Us




Windows 10 Forums