Search Registry and create New-Item












2















I want to create a New-Item in the registry with a pre check what already exists.



This code



$items = get-item "HKCU:SOFTWAREMicrosoftOffice16.0ExcelOptions" 
$items.Property -match "OPEN"


returns the following



OPEN
OPEN1
OPEN2
OPEN3
OPEN4


Now I know I need to create a New-Item with the name OPEN5, but how do I count through this? Maybe with a While-Loop?










share|improve this question























  • You could do something like for ($i = 0; $i -lt $items.Count; $i++ { # implementation code here }

    – trebleCode
    Nov 13 '18 at 15:34
















2















I want to create a New-Item in the registry with a pre check what already exists.



This code



$items = get-item "HKCU:SOFTWAREMicrosoftOffice16.0ExcelOptions" 
$items.Property -match "OPEN"


returns the following



OPEN
OPEN1
OPEN2
OPEN3
OPEN4


Now I know I need to create a New-Item with the name OPEN5, but how do I count through this? Maybe with a While-Loop?










share|improve this question























  • You could do something like for ($i = 0; $i -lt $items.Count; $i++ { # implementation code here }

    – trebleCode
    Nov 13 '18 at 15:34














2












2








2


1






I want to create a New-Item in the registry with a pre check what already exists.



This code



$items = get-item "HKCU:SOFTWAREMicrosoftOffice16.0ExcelOptions" 
$items.Property -match "OPEN"


returns the following



OPEN
OPEN1
OPEN2
OPEN3
OPEN4


Now I know I need to create a New-Item with the name OPEN5, but how do I count through this? Maybe with a While-Loop?










share|improve this question














I want to create a New-Item in the registry with a pre check what already exists.



This code



$items = get-item "HKCU:SOFTWAREMicrosoftOffice16.0ExcelOptions" 
$items.Property -match "OPEN"


returns the following



OPEN
OPEN1
OPEN2
OPEN3
OPEN4


Now I know I need to create a New-Item with the name OPEN5, but how do I count through this? Maybe with a While-Loop?







powershell while-loop registry new-item






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 14:52









aston_zhaston_zh

1,93331119




1,93331119













  • You could do something like for ($i = 0; $i -lt $items.Count; $i++ { # implementation code here }

    – trebleCode
    Nov 13 '18 at 15:34



















  • You could do something like for ($i = 0; $i -lt $items.Count; $i++ { # implementation code here }

    – trebleCode
    Nov 13 '18 at 15:34

















You could do something like for ($i = 0; $i -lt $items.Count; $i++ { # implementation code here }

– trebleCode
Nov 13 '18 at 15:34





You could do something like for ($i = 0; $i -lt $items.Count; $i++ { # implementation code here }

– trebleCode
Nov 13 '18 at 15:34












1 Answer
1






active

oldest

votes


















1














The most robust approach is to extract the embedded numbers, sort them numerically, and add 1 to the highest index to date:



$nextNdx = 1 +
([int] ($items.Property -match '^OPEN' -replace 'D') | Sort-Object)[-1]



  • $items.Property -match '^OPEN' -replace 'D' returns all property names that start with OPEN and removes all non-digit characters from them (-replace 'D').


  • [int] converts the resulting "number strings" to actual numbers ([int]); note that casting '' or $null to [int] in PowerShell yields 0.


  • Sort-Object sorts these numbers, and [-1] grabs the last number from the resulting array, i.e., the highest number.





The above is convenient, but not fast, due to use of the pipeline and the Sort-Object cmdlet.



If you want to avoid the pipeline for performance reasons:



$indices = [int] ($items.Property -match '^OPEN' -replace 'D')
[Array]::Sort($indices) # sort in place
$nextNdx = 1 + $indices[-1]





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53283682%2fsearch-registry-and-create-new-item%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    The most robust approach is to extract the embedded numbers, sort them numerically, and add 1 to the highest index to date:



    $nextNdx = 1 +
    ([int] ($items.Property -match '^OPEN' -replace 'D') | Sort-Object)[-1]



    • $items.Property -match '^OPEN' -replace 'D' returns all property names that start with OPEN and removes all non-digit characters from them (-replace 'D').


    • [int] converts the resulting "number strings" to actual numbers ([int]); note that casting '' or $null to [int] in PowerShell yields 0.


    • Sort-Object sorts these numbers, and [-1] grabs the last number from the resulting array, i.e., the highest number.





    The above is convenient, but not fast, due to use of the pipeline and the Sort-Object cmdlet.



    If you want to avoid the pipeline for performance reasons:



    $indices = [int] ($items.Property -match '^OPEN' -replace 'D')
    [Array]::Sort($indices) # sort in place
    $nextNdx = 1 + $indices[-1]





    share|improve this answer






























      1














      The most robust approach is to extract the embedded numbers, sort them numerically, and add 1 to the highest index to date:



      $nextNdx = 1 +
      ([int] ($items.Property -match '^OPEN' -replace 'D') | Sort-Object)[-1]



      • $items.Property -match '^OPEN' -replace 'D' returns all property names that start with OPEN and removes all non-digit characters from them (-replace 'D').


      • [int] converts the resulting "number strings" to actual numbers ([int]); note that casting '' or $null to [int] in PowerShell yields 0.


      • Sort-Object sorts these numbers, and [-1] grabs the last number from the resulting array, i.e., the highest number.





      The above is convenient, but not fast, due to use of the pipeline and the Sort-Object cmdlet.



      If you want to avoid the pipeline for performance reasons:



      $indices = [int] ($items.Property -match '^OPEN' -replace 'D')
      [Array]::Sort($indices) # sort in place
      $nextNdx = 1 + $indices[-1]





      share|improve this answer




























        1












        1








        1







        The most robust approach is to extract the embedded numbers, sort them numerically, and add 1 to the highest index to date:



        $nextNdx = 1 +
        ([int] ($items.Property -match '^OPEN' -replace 'D') | Sort-Object)[-1]



        • $items.Property -match '^OPEN' -replace 'D' returns all property names that start with OPEN and removes all non-digit characters from them (-replace 'D').


        • [int] converts the resulting "number strings" to actual numbers ([int]); note that casting '' or $null to [int] in PowerShell yields 0.


        • Sort-Object sorts these numbers, and [-1] grabs the last number from the resulting array, i.e., the highest number.





        The above is convenient, but not fast, due to use of the pipeline and the Sort-Object cmdlet.



        If you want to avoid the pipeline for performance reasons:



        $indices = [int] ($items.Property -match '^OPEN' -replace 'D')
        [Array]::Sort($indices) # sort in place
        $nextNdx = 1 + $indices[-1]





        share|improve this answer















        The most robust approach is to extract the embedded numbers, sort them numerically, and add 1 to the highest index to date:



        $nextNdx = 1 +
        ([int] ($items.Property -match '^OPEN' -replace 'D') | Sort-Object)[-1]



        • $items.Property -match '^OPEN' -replace 'D' returns all property names that start with OPEN and removes all non-digit characters from them (-replace 'D').


        • [int] converts the resulting "number strings" to actual numbers ([int]); note that casting '' or $null to [int] in PowerShell yields 0.


        • Sort-Object sorts these numbers, and [-1] grabs the last number from the resulting array, i.e., the highest number.





        The above is convenient, but not fast, due to use of the pipeline and the Sort-Object cmdlet.



        If you want to avoid the pipeline for performance reasons:



        $indices = [int] ($items.Property -match '^OPEN' -replace 'D')
        [Array]::Sort($indices) # sort in place
        $nextNdx = 1 + $indices[-1]






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 13 '18 at 16:03

























        answered Nov 13 '18 at 15:46









        mklement0mklement0

        128k20242271




        128k20242271






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53283682%2fsearch-registry-and-create-new-item%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues