Windows 10: PowerShell Scripting - The Basics  

Page 2 of 3 FirstFirst 123 LastLast

  1. Posts : 38,043
    Dual boot Windows 10 FCU Pro x 64 & current Insider 10 Pro
       24 Oct 2017 #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 ComputersSystem Spec

  2.    24 Oct 2017 #11

    Kari said: View Post
    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 ComputerSystem Spec

  3.    24 Oct 2017 #12

    slicendice said: View Post
    @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 ComputerSystem Spec


  4. Posts : 14,248
    Windows 10 Pro
    Thread Starter
       24 Oct 2017 #13

    HippsieGypsie said: View Post
    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:
    Name:  image.png
Views: 163
Size:  283.0 KB


    HippsieGypsie said: View Post
    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 ComputerSystem Spec


  5. Posts : 14,248
    Windows 10 Pro
    Thread Starter
       24 Oct 2017 #14

    cereberus said: View Post
    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 ComputerSystem Spec


  6. Posts : 14,248
    Windows 10 Pro
    Thread Starter
       24 Oct 2017 #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 ComputerSystem Spec


  7. Posts : 31
    Windows 10 Pro for Workstations
       26 Oct 2017 #16

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


  8. Posts : 3,232
    Windows 10 Pro x64 v1803 Build 17134.137 (Branch: RS4 Release Preview)
       27 Oct 2017 #17

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

  9.    27 Oct 2017 #18

    Kari said: View Post
    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 ComputerSystem Spec


  10. Posts : 14,248
    Windows 10 Pro
    Thread Starter
       27 Oct 2017 #19

    DrEmpiricism said: View Post
    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: View Post
    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 ComputerSystem Spec


 
Page 2 of 3 FirstFirst 123 LastLast

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 Xbox Windows Phone


Related Threads
The way Windows operating system is delivered and installed has changed quite a lot from days when Windows 3.0 was delivered on 6 floppy disks. To install, you booted PC from floppy 1, which in its turn asked you to eject it and insert floppy 2 to...
Scripting Errors in Performance & Maintenance
I had this problem a few months ago and it went away by itself. I tried This morning some restarts and also the tools. internet ops, advanced with boxes checked. It seems to appear more when I go to a newspaper site, Wall Street Journal, New...
Back when I got Windows 10 I started having this problem with iTunes that I didn't have before with 7. When I go to close iTunes it freezes and then this message pops up, "One or more applications are using the iTunes scripting interface. Are you...
Solved Scripting hulu plus in General Support
Good morning. I was trying to create a script to run hulu plus from the microsoft store. So I made a simple .bat file with Start hulu plus:. This command will run non stop it just keeps repeating itself in a loop. I used the command for Netflix and...
Source: http://blogs.windows.com/msedgedev/2015/05/18/using-chakra-for-scripting-applications-across-windows-10/
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 06:02.
Find Us