Run Multiple Commands In Powershell?


  1. Posts : 671
    Win 10 21H1 (OS Build 19043.1151)
       #1

    Run Multiple Commands In Powershell?


    Hi,

    I have three drives in my PC.
    Two of them are SSD's and the third one is a mechanical HDD.
    I like to optimize them using Powershell.

    However, as it stands now.
    I'm running the commands separately for each of my drive, i.e.

    1.) Optimize-Volume -DriveLetter C -ReTrim -Verbose
    For optimizing the (M.2 NVME) C:\ drive.

    2 .) Optimize-Volume -DriveLetter D -Verbose
    To optimize the (Mechanical HDD) D:\ drive.

    3.)Optimize-Volume -DriveLetter E -ReTrim -Verbose
    For the (SSD 850 EVO) E:\ drive.

    Is there a way to combine these 3 commands into one command.
    Which will run in Powershell.
    Without having to enter/type each of the commands separately?

    Thanks.
      My Computer


  2. Posts : 4,224
    Windows 10
       #2

    You can use a semicolon to string multiple PowerShell commands together onto a single line of text. Thus, for example, you could release and renew a DHCP release in PowerShell by entering

    Code:
    ipconfig /release; ipconfig /renew
    on a single line of text. This will run the commands sequentially but will run all remaining subsequent commands, even if one command fails to execute properly. Use with caution.

    HTH,
    --Ed--
    Last edited by EdTittel; 02 May 2018 at 09:53. Reason: Fix bad markup
      My Computers


  3. Posts : 671
    Win 10 21H1 (OS Build 19043.1151)
    Thread Starter
       #3

    @EdTittel

    EdTittel said:
    You can use a semicolon to string multiple PowerShell commands together onto a single line of text. Thus, for example, you could release and renew a DHCP release in PowerShell by entering

    Code:
    ipconfig /release; ipconfig /renew
    on a single line of text. This will run the commands sequentially but will run all remaining subsequent commands, even if one command fails to execute properly. Use with caution.

    HTH,
    --Ed--
    This is the three commands I combined by using a semicolon.

    Code:
    Optimize-Volume -DriveLetter C -ReTrim -Verbose; Optimize-Volume -DriveLetter E -ReTrim -Verbose; Optimize-Volume -DriveLetter D -Verbose
    It worked out great!
    Now I won't have to do each drive separately.
    I tried searching this, but could not find any real direct answers.
    Perhaps I typed in the wrong key words in my search.
    Nonetheless,
    thanks a bunch Mr.EdTittel
      My Computer


  4. Posts : 5,478
    2004
       #4

    Alternatively if the commands were the same you could just run one command instead of 3 and run in parallel by listing the drives separated by commas Optimize-Volume -DriveLetter C,D,E -ReTrim -Verbose
    Code:
    PS C:\Windows\system32> Optimize-Volume -DriveLetter C,D,E -ReTrim -Verbose
    VERBOSE: Invoking retrim on Rescue (E:)...
    VERBOSE: Performing pass 1:
    VERBOSE: Invoking retrim on Windows (C:)...
    VERBOSE: Retrim:  0% complete...
    VERBOSE: Invoking retrim on (D:)...
    VERBOSE: Retrim:  0% complete...
    VERBOSE: Retrim:  100% complete.
    VERBOSE: Retrim:  0% complete...
    VERBOSE: Retrim:  100% complete.
    VERBOSE:
    Post Defragmentation Report:
    VERBOSE: Retrim:  100% complete.
    VERBOSE:
    Post Defragmentation Report:
    VERBOSE:
    Post Defragmentation Report:
    VERBOSE:
     Volume Information:
    VERBOSE:
     Volume Information:
    VERBOSE:
     Volume Information:
    VERBOSE:   Volume size                 = 4.38 GB
    VERBOSE:   Volume size                 = 63.90 GB
    VERBOSE:   Volume size                 = 164.00 GB
    VERBOSE:   Cluster size                = 4 KB
    VERBOSE:   Cluster size                = 4 KB
    VERBOSE:   Cluster size                = 4 KB
    VERBOSE:   Used space                  = 1.54 GB
    VERBOSE:   Used space                  = 125.11 GB
    VERBOSE:   Used space                  = 34.85 GB
    VERBOSE:   Free space                  = 2.84 GB
    VERBOSE:   Free space                  = 38.88 GB
    VERBOSE:   Free space                  = 29.04 GB
    VERBOSE:
     Retrim:
    VERBOSE:
     Retrim:
    VERBOSE:
     Retrim:
    VERBOSE:   Backed allocations          = 4
    VERBOSE:   Allocations trimmed         = 37
    VERBOSE:   Backed allocations          = 164
    VERBOSE:   Backed allocations          = 63
    VERBOSE:   Total space trimmed         = 968.72 MB
    VERBOSE:   Allocations trimmed         = 292
    VERBOSE:   Allocations trimmed         = 477
    VERBOSE:   Total space trimmed         = 37.60 GB
    VERBOSE:   Total space trimmed         = 26.73 GB
    PS C:\Windows\system32>
    Or perhaps use (again via powershell) defrag /c /h /o as this will run the correct optimization (trim/slab consolidate/defrag depending if it is SSD/VHD/HDD) for any connected volumes without demanding their names.

    This is how I do it anyway and you can see it ran retrim on SSD volumes and defragmentation of F: (which is a HDD)...
    Code:
    PS C:\Windows\system32> defrag /c /h /o
    Microsoft Drive Optimizer
    Copyright (c) Microsoft Corp.
    
    Invoking retrim on System...
    
            Retrim:  100% complete.
    
    The operation completed successfully.
    
    Post Defragmentation Report:
    
            Volume Information:
                    Volume size                 = 99.99 MB
                    Free space                  = 73.63 MB
    
            Retrim:
                    Total space trimmed         = 0 bytes
    
    Invoking defragmentation on Dock (F:)...
    
    
    Pre-Optimization Report:
    
            Volume Information:
                    Volume size                 = 465.75 GB
                    Free space                  = 142.28 GB
                    Total fragmented space      = 0%
                    Largest free space size     = 142.13 GB
    
            Note: File fragments larger than 64MB are not included in the fragmentation statistics.
    
    The operation completed successfully.
    
    Post Defragmentation Report:
    
            Volume Information:
                    Volume size                 = 465.75 GB
                    Free space                  = 142.28 GB
                    Total fragmented space      = 0%
                    Largest free space size     = 142.13 GB
    
            Note: File fragments larger than 64MB are not included in the fragmentation statistics.
    
    Invoking retrim on (D:)...
    
    
    The operation completed successfully.
    
    Post Defragmentation Report:
    
            Volume Information:
                    Volume size                 = 164.00 GB
                    Free space                  = 38.88 GB
    
            Retrim:
                    Total space trimmed         = 37.60 GB
    
    Invoking retrim on Rescue (E:)...
    
            Retrim:  100% complete.
    
    The operation completed successfully.
    
    Post Defragmentation Report:
    
            Volume Information:
                    Volume size                 = 4.38 GB
                    Free space                  = 2.84 GB
    
            Retrim:
                    Total space trimmed         = 968.72 MB
    
    Invoking retrim on Windows (C:)...
    
    
    The operation completed successfully.
    
    Post Defragmentation Report:
    
            Volume Information:
                    Volume size                 = 63.90 GB
                    Free space                  = 29.04 GB
    
            Retrim:
                    Total space trimmed         = 26.73 GB
    PS C:\Windows\system32>
    Many ways to do things :)
      My Computer


  5. Posts : 671
    Win 10 21H1 (OS Build 19043.1151)
    Thread Starter
       #5

    @lx07

    lx07 said:
    Alternatively if the commands were the same you could just run one command instead of 3 and run in parallel by listing the drives separated by commas Optimize-Volume -DriveLetter C,D,E -ReTrim -Verbose
    Code:
    PS C:\Windows\system32> Optimize-Volume -DriveLetter C,D,E -ReTrim -Verbose
    VERBOSE: Invoking retrim on Rescue (E:)...
    VERBOSE: Performing pass 1:
    VERBOSE: Invoking retrim on Windows (C:)...
    VERBOSE: Retrim:  0% complete...
    VERBOSE: Invoking retrim on (D:)...
    VERBOSE: Retrim:  0% complete...
    VERBOSE: Retrim:  100% complete.
    VERBOSE: Retrim:  0% complete...
    VERBOSE: Retrim:  100% complete.
    VERBOSE:
    Post Defragmentation Report:
    VERBOSE: Retrim:  100% complete.
    VERBOSE:
    Post Defragmentation Report:
    VERBOSE:
    Post Defragmentation Report:
    VERBOSE:
     Volume Information:
    VERBOSE:
     Volume Information:
    VERBOSE:
     Volume Information:
    VERBOSE:   Volume size                 = 4.38 GB
    VERBOSE:   Volume size                 = 63.90 GB
    VERBOSE:   Volume size                 = 164.00 GB
    VERBOSE:   Cluster size                = 4 KB
    VERBOSE:   Cluster size                = 4 KB
    VERBOSE:   Cluster size                = 4 KB
    VERBOSE:   Used space                  = 1.54 GB
    VERBOSE:   Used space                  = 125.11 GB
    VERBOSE:   Used space                  = 34.85 GB
    VERBOSE:   Free space                  = 2.84 GB
    VERBOSE:   Free space                  = 38.88 GB
    VERBOSE:   Free space                  = 29.04 GB
    VERBOSE:
     Retrim:
    VERBOSE:
     Retrim:
    VERBOSE:
     Retrim:
    VERBOSE:   Backed allocations          = 4
    VERBOSE:   Allocations trimmed         = 37
    VERBOSE:   Backed allocations          = 164
    VERBOSE:   Backed allocations          = 63
    VERBOSE:   Total space trimmed         = 968.72 MB
    VERBOSE:   Allocations trimmed         = 292
    VERBOSE:   Allocations trimmed         = 477
    VERBOSE:   Total space trimmed         = 37.60 GB
    VERBOSE:   Total space trimmed         = 26.73 GB
    PS C:\Windows\system32>
    Or perhaps use (again via powershell) defrag /c /h /o as this will run the correct optimization (trim/slab consolidate/defrag depending if it is SSD/VHD/HDD) for any connected volumes without demanding their names.

    This is how I do it anyway...
    Code:
    PS C:\Windows\system32> defrag /c /h /o
    Microsoft Drive Optimizer
    Copyright (c) Microsoft Corp.
    
    Invoking retrim on System...
    
            Retrim:  100% complete.
    
    The operation completed successfully.
    
    Post Defragmentation Report:
    
            Volume Information:
                    Volume size                 = 99.99 MB
                    Free space                  = 73.63 MB
    
            Retrim:
                    Total space trimmed         = 0 bytes
    
    Invoking retrim on (D:)...
    
    
    The operation completed successfully.
    
    Post Defragmentation Report:
    
            Volume Information:
                    Volume size                 = 164.00 GB
                    Free space                  = 38.88 GB
    
            Retrim:
                    Total space trimmed         = 37.60 GB
    
    Invoking retrim on Rescue (E:)...
    
            Retrim:  100% complete.
    
    The operation completed successfully.
    
    Post Defragmentation Report:
    
            Volume Information:
                    Volume size                 = 4.38 GB
                    Free space                  = 2.84 GB
    
            Retrim:
                    Total space trimmed         = 968.72 MB
    
    Invoking retrim on Windows (C:)...
    
    
    The operation completed successfully.
    
    Post Defragmentation Report:
    
            Volume Information:
                    Volume size                 = 63.90 GB
                    Free space                  = 29.04 GB
    
            Retrim:
                    Total space trimmed         = 26.73 GB
    PS C:\Windows\system32>
    Many ways to do things :)
    Thank you.
    That's a much simpler command line.
      My Computer


  6. Posts : 4,224
    Windows 10
       #6

    Good job, @lx07. Of course, you had the benefit of knowing the OP wanted to run the same command on 3 targets. Had I been armed with that same information, I might have made that same suggestion, too. That's why we're better working together than working individually.
    --Ed--
      My Computers


 

  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 00:17.
Find Us




Windows 10 Forums