Script that spits our random command problem [Powershell]2019 Community Moderator ElectionEquivalent of *Nix 'which' command in PowerShell?How to recursively delete an entire directory with PowerShell 2.0?Powershell: Is it possible to set-alias on a piped command?Terminating a script in PowerShellHow to run a PowerShell scriptHow to handle command-line arguments in PowerShellPowerShell says “execution of scripts is disabled on this system.”What's the best way to determine the location of the current PowerShell script?How to pass an argument to a PowerShell script?Powershell script appears to execute command with wildcard
Use Mercury as quenching liquid for swords?
Book where society has been split into 2 with a wall down the middle where one side embraced high tech whereas other side were totally against tech
Ultrafilters as a double dual
Should we avoid writing fiction about historical events without extensive research?
Should I apply for my boss's promotion?
Why is there an extra space when I type "ls" on the Desktop?
Averaging over columns while ignoring zero entries
What exactly is the meaning of "fine wine"?
Short story about cities being connected by a conveyor belt
What is the purpose of a disclaimer like "this is not legal advice"?
Giving a talk in my old university, how prominently should I tell students my salary?
What is better: yes / no radio, or simple checkbox?
Inorganic chemistry handbook with reaction lists
How to install "rounded" brake pads
I am the light that shines in the dark
3.5% Interest Student Loan or use all of my savings on Tuition?
ESPP--any reason not to go all in?
Was this cameo in Captain Marvel computer generated?
Tool for measuring readability of English text
Professor forcing me to attend a conference, I can't afford even with 50% funding
Will the concrete slab in a partially heated shed conduct a lot of heat to the unconditioned area?
Boss Telling direct supervisor I snitched
What would be the most expensive material to an intergalactic society?
Is "cogitate" used appropriately in "I cogitate that success relies on hard work"?
Script that spits our random command problem [Powershell]
2019 Community Moderator ElectionEquivalent of *Nix 'which' command in PowerShell?How to recursively delete an entire directory with PowerShell 2.0?Powershell: Is it possible to set-alias on a piped command?Terminating a script in PowerShellHow to run a PowerShell scriptHow to handle command-line arguments in PowerShellPowerShell says “execution of scripts is disabled on this system.”What's the best way to determine the location of the current PowerShell script?How to pass an argument to a PowerShell script?Powershell script appears to execute command with wildcard
$k = get-command -all | measure | select-object count
$result = $k -replace "[@Count=]", ""
$rand = get-random -maximum $result
$minrand = $rand - 1
get-command -all -totalcount $rand | Select-Object -skip $minrand
This is supposed to spit out ONE command, alias, cmdlet whatever. I would hope that this would be useful for learning PowerShell.
The issue is that it spits out the same command over and over.
powershell command cmdlet
New contributor
add a comment |
$k = get-command -all | measure | select-object count
$result = $k -replace "[@Count=]", ""
$rand = get-random -maximum $result
$minrand = $rand - 1
get-command -all -totalcount $rand | Select-Object -skip $minrand
This is supposed to spit out ONE command, alias, cmdlet whatever. I would hope that this would be useful for learning PowerShell.
The issue is that it spits out the same command over and over.
powershell command cmdlet
New contributor
1
PipeGet-Random
toGet-Command
-->Get-Command | Get-Random
– Vivek Kumar Singh
2 days ago
add a comment |
$k = get-command -all | measure | select-object count
$result = $k -replace "[@Count=]", ""
$rand = get-random -maximum $result
$minrand = $rand - 1
get-command -all -totalcount $rand | Select-Object -skip $minrand
This is supposed to spit out ONE command, alias, cmdlet whatever. I would hope that this would be useful for learning PowerShell.
The issue is that it spits out the same command over and over.
powershell command cmdlet
New contributor
$k = get-command -all | measure | select-object count
$result = $k -replace "[@Count=]", ""
$rand = get-random -maximum $result
$minrand = $rand - 1
get-command -all -totalcount $rand | Select-Object -skip $minrand
This is supposed to spit out ONE command, alias, cmdlet whatever. I would hope that this would be useful for learning PowerShell.
The issue is that it spits out the same command over and over.
powershell command cmdlet
powershell command cmdlet
New contributor
New contributor
New contributor
asked 2 days ago
Preben molandPreben moland
183
183
New contributor
New contributor
1
PipeGet-Random
toGet-Command
-->Get-Command | Get-Random
– Vivek Kumar Singh
2 days ago
add a comment |
1
PipeGet-Random
toGet-Command
-->Get-Command | Get-Random
– Vivek Kumar Singh
2 days ago
1
1
Pipe
Get-Random
to Get-Command
--> Get-Command | Get-Random
– Vivek Kumar Singh
2 days ago
Pipe
Get-Random
to Get-Command
--> Get-Command | Get-Random
– Vivek Kumar Singh
2 days ago
add a comment |
1 Answer
1
active
oldest
votes
Your code is very complicated. You can just do this:
$Commands = Get-Command -All
Then keep running this:
Get-Random $Commands
To get a random different command each time.
Regarding your code, you don't ever need to do this (which would return a string result):
$k = get-command -all | measure | select-object count
$result = $k -replace "[@Count=]", ""
You should instead be doing something like this:
$k = get-command -all | measure | select-object count
$result = $k.count
Via which you're accessing the count property of $k
and getting its integer value.
PowerShell returns objects with properties, so while you often see string based results in the console, when you want to manipulate those results you should work with the object properties. Piping an object to Get-Member
is a good way to discover the properties of an object (as well as its methods etc.). For example try:
$k | Get-Member
To see its properties.
Get-Help
, Get-Command
and Get-Member
are 3 of the most useful tools for discovering and learning PowerShell from within the shell.
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
);
);
Preben moland is a new contributor. Be nice, and check out our Code of Conduct.
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%2f55023541%2fscript-that-spits-our-random-command-problem-powershell%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
Your code is very complicated. You can just do this:
$Commands = Get-Command -All
Then keep running this:
Get-Random $Commands
To get a random different command each time.
Regarding your code, you don't ever need to do this (which would return a string result):
$k = get-command -all | measure | select-object count
$result = $k -replace "[@Count=]", ""
You should instead be doing something like this:
$k = get-command -all | measure | select-object count
$result = $k.count
Via which you're accessing the count property of $k
and getting its integer value.
PowerShell returns objects with properties, so while you often see string based results in the console, when you want to manipulate those results you should work with the object properties. Piping an object to Get-Member
is a good way to discover the properties of an object (as well as its methods etc.). For example try:
$k | Get-Member
To see its properties.
Get-Help
, Get-Command
and Get-Member
are 3 of the most useful tools for discovering and learning PowerShell from within the shell.
add a comment |
Your code is very complicated. You can just do this:
$Commands = Get-Command -All
Then keep running this:
Get-Random $Commands
To get a random different command each time.
Regarding your code, you don't ever need to do this (which would return a string result):
$k = get-command -all | measure | select-object count
$result = $k -replace "[@Count=]", ""
You should instead be doing something like this:
$k = get-command -all | measure | select-object count
$result = $k.count
Via which you're accessing the count property of $k
and getting its integer value.
PowerShell returns objects with properties, so while you often see string based results in the console, when you want to manipulate those results you should work with the object properties. Piping an object to Get-Member
is a good way to discover the properties of an object (as well as its methods etc.). For example try:
$k | Get-Member
To see its properties.
Get-Help
, Get-Command
and Get-Member
are 3 of the most useful tools for discovering and learning PowerShell from within the shell.
add a comment |
Your code is very complicated. You can just do this:
$Commands = Get-Command -All
Then keep running this:
Get-Random $Commands
To get a random different command each time.
Regarding your code, you don't ever need to do this (which would return a string result):
$k = get-command -all | measure | select-object count
$result = $k -replace "[@Count=]", ""
You should instead be doing something like this:
$k = get-command -all | measure | select-object count
$result = $k.count
Via which you're accessing the count property of $k
and getting its integer value.
PowerShell returns objects with properties, so while you often see string based results in the console, when you want to manipulate those results you should work with the object properties. Piping an object to Get-Member
is a good way to discover the properties of an object (as well as its methods etc.). For example try:
$k | Get-Member
To see its properties.
Get-Help
, Get-Command
and Get-Member
are 3 of the most useful tools for discovering and learning PowerShell from within the shell.
Your code is very complicated. You can just do this:
$Commands = Get-Command -All
Then keep running this:
Get-Random $Commands
To get a random different command each time.
Regarding your code, you don't ever need to do this (which would return a string result):
$k = get-command -all | measure | select-object count
$result = $k -replace "[@Count=]", ""
You should instead be doing something like this:
$k = get-command -all | measure | select-object count
$result = $k.count
Via which you're accessing the count property of $k
and getting its integer value.
PowerShell returns objects with properties, so while you often see string based results in the console, when you want to manipulate those results you should work with the object properties. Piping an object to Get-Member
is a good way to discover the properties of an object (as well as its methods etc.). For example try:
$k | Get-Member
To see its properties.
Get-Help
, Get-Command
and Get-Member
are 3 of the most useful tools for discovering and learning PowerShell from within the shell.
edited 2 days ago
answered 2 days ago
Mark WraggMark Wragg
14.5k42145
14.5k42145
add a comment |
add a comment |
Preben moland is a new contributor. Be nice, and check out our Code of Conduct.
Preben moland is a new contributor. Be nice, and check out our Code of Conduct.
Preben moland is a new contributor. Be nice, and check out our Code of Conduct.
Preben moland is a new contributor. Be nice, and check out our Code of Conduct.
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%2f55023541%2fscript-that-spits-our-random-command-problem-powershell%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
1
Pipe
Get-Random
toGet-Command
-->Get-Command | Get-Random
– Vivek Kumar Singh
2 days ago