Generate random numbers in PowerShell with Get-Random
Have you ever wanted to generate random numbers for passwords, test data…. and wondered how you could easily generate them?
Well I was confronted by the same situation on one of our servers where I needed some random numbers for inputting wait times to test an application but didn’t have my usual tool of choice ( MS Excel ) available to me.
I remembered the Get-Random command from memory but used the Get-Help command to allow me to use the correct syntax.
Executing Get-Random will produce a random number.
This was quite a good start but then I needed only 4 digit figures and I then had to use the minimum and maximum parameters.
Get-Random -Maximum 9999 -Minimum 1000
The above command solved that problem for me and I think it reflects how easily readable the PowerShell language is.
The last scenario I had was producing random numbers for some temporary passwords. I looked at the help for Get-Command and noticed the inputobject and count parameters which I thought would fulfil my need.
Get-Random -InputObject (10000..99999) -Count 3
The above command shows the range of values which I gave and the number of random numbers I needed in the Count parameter.
Hopefully the above will help you with your random number needs. Please add your feedback or other solutions in the comments below.
Have you ever wondered what updates have been applied to your Windows operating system?
Well a quick and easy way to do that is to use the PowerShell command Get-Hotfix. This will list out the description, the HotfixID and when it was installed.
I used this on a few PCs where it appeared that updates were not being applied and it showed me when the last updates were installed and hence helped me identify the PCs which were having the trouble and disconnect them from the network.
Have you ever wandered whether you could speed up your script writing,shorten down your snippets or come across strange commands in PowerShell?
Well PowerShell’s Aliases would be the answer to all the above scenarios. I first came across them when I saw it in a friends script and could not ascertain what GDR was and is later transpired to be GET-PSDRIVE.
Though I don’t use them in my own scripts as it makes it harder to read, debug and modify. I do believe everyone should familiarise themselves with the alias commands and have listed them below.
Have you wanted to output results from PowerShell to CSV or XML? Well I am going to show you how.
I have previously explained how to export results of PowerShell queries but did not include how to export the results to CSV or XML. The reason for that is that when you become familiar with PowerShell you will find out that PowerShell commands can return a lot more information than what they show on screen. I will show how to get that information in a future blog post but just be aware that there is a lot more information readily available and only the displayed columns are exported to text(TXT) files.
When exporting to CSV and XML the hidden columns are exported as well. I will give an example of the difference in the exports.
The results of Get-Process are displayed below.
If I run the below commands I would output the results of Get-Process to CSV and XML.
Have you ever wanted to open a file and view its contents using PowerShell? Well I am going to show you how.
I frequently output the results of PowerShell queries into files for later review but rarely used PowerShell to view the contents. This has been because I always had other programs readily available to view the contents easily so never thought to use it. The other day I was on a server where we didn’t have access to notepad and I wanted to look at a file I had generated earlier that day.
I used an old DOS command ‘type’ but saw my colleague who frequently worked on that server use a PowerShell command I was unfamiliar with. He was using Get-Content to look at some files so I used the help command to quickly familiarise myself with the command and once again I was smiling at the beauty and ease of PowerShell commands.
Have you ever wanted to send the output of a PowerShell command to a file? Well I am going to show you how.
When you run a PowerShell command like Get-Process, Get-Host…. It will return useful information but sometimes you want to save that information for future reference. You could cut and paste or take a screenshot but these are way more complicated than simply using the inbuilt functionality within PowerShell.
For example if you wanted to output the processes currently running you would use Get-Process. To output it you would add the below command
Have you ever wanted to quickly find the processes running using Powershell? Wanted an alternate to Task Manager? Well I am going to show you how.
Whenever I get a call from someone about a performance issue on any of our SQL Servers I always usually have a quick glance of Task Manager so that I can help identify what maybe causing the problem. However, the problem can be that on a server which is low on resources it may take a long time to open or the opening of task manager may add to the problems or it may not open at all.
This has led me to frequently opening PowerShell and using it to get the information which I need. The script is quite simple and is below.
I have found the advantage of using the script is that it gives me a snapshot whilst in task manager it is always a moving target and if I run it several times I can just scroll up and down to find a pattern which would help diagnose the problem.
Have you ever wanted to get all the operating system properties of a computer? Well I am going to show you a short PowerShell script which can get loads of information about the operating system.
One of my colleagues was googling around for information about how to find the windows directory, number of users, last boot up time and many other properties of various computers. I told him that I had a short script which had that information and probably a whole lot more information which he might find useful.
A quick snippet for some newbie PowerShell developers whilst they start getting their teeth into the ISE is there are two easy ways of clearing the screen. I ahdnt thought about sharing this information but I saw a developer simply closing the ISE so that he could start afresh. I asked him why and he didn’t know any other way and hadn’t spent any time exploring the ISE or learning many PowerShell commands.
If you ever have a screen which you want to clear like below.
You can either type cls which will clear the screen or if you forget that syntax use the clear console pane button highlighted below.