Search Registry and create New-Item
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
add a comment |
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
You could do something likefor ($i = 0; $i -lt $items.Count; $i++ { # implementation code here }
– trebleCode
Nov 13 '18 at 15:34
add a comment |
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
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
powershell while-loop registry new-item
asked Nov 13 '18 at 14:52
aston_zhaston_zh
1,93331119
1,93331119
You could do something likefor ($i = 0; $i -lt $items.Count; $i++ { # implementation code here }
– trebleCode
Nov 13 '18 at 15:34
add a comment |
You could do something likefor ($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
add a comment |
1 Answer
1
active
oldest
votes
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 withOPEN
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 yields0
.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]
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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 withOPEN
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 yields0
.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]
add a comment |
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 withOPEN
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 yields0
.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]
add a comment |
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 withOPEN
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 yields0
.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]
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 withOPEN
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 yields0
.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]
edited Nov 13 '18 at 16:03
answered Nov 13 '18 at 15:46
mklement0mklement0
128k20242271
128k20242271
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
You could do something like
for ($i = 0; $i -lt $items.Count; $i++ { # implementation code here }
– trebleCode
Nov 13 '18 at 15:34