Windows 10: Run Multiple Commands In Powershell? Solved

  1. Geosammy's Avatar
    Posts : 450
    Win10 Home 64 bit, Ver 1809 (OS Build 17763.55)
       01 May 2018 #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 ComputerSystem Spec

  2.    02 May 2018 #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 ComputersSystem Spec

  3. Geosammy's Avatar
    Posts : 450
    Win10 Home 64 bit, Ver 1809 (OS Build 17763.55)
    Thread Starter
       02 May 2018 #3

    @EdTittel

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

  4.    02 May 2018 #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 ComputerSystem Spec

  5. Geosammy's Avatar
    Posts : 450
    Win10 Home 64 bit, Ver 1809 (OS Build 17763.55)
    Thread Starter
       02 May 2018 #5

    @lx07

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

  6.    03 May 2018 #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 ComputersSystem Spec


 

Related Threads
A friend has had nearly 3000 Powershell crashes logged ever since build 16299.248 was installed. Powershell crashes spontaneously, possibly when a background task starts. I believe build 248 has introduced a new bug. Q1) Does anyone have a...
A friend is getting many Powershell errors 181887 Often 20 or more interruptions per session. The Application Event log shows many sets of 4 events Error 22/03/2018 13:05:08 Application Error 1000 (100) Error 22/03/2018...
I have some code written that works on text files. It can work on multiple files. Something like this: myprogram.exe myfile1.txt myfile2.txt myfile3.txt I want to have a Windows interface so I can run my code as described. I have a...
I have spent many hours for the last two days trying to get the Store and several apps re-registered and working. On other forums which I have been directed to, such as MS TechNet, much of the advice and links I have been given either generate...
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 16:53.
Find Us