Understanding different (Six and more!) PowerShell profiles

As you know, PowerShell profiles are a great way to store persistent PowerShell settings which applies whenever you start a new session. So, if a profile is good, wouldn’t it be good if you can have multiple profiles. It turns out that any single PowerShell session may use a number of profiles and different PowerShell hosts provide yet more choices. By default, PowerShell (and ISE) provides six different choices out of the box. In this blog post, we are going to understand these one.Read More »

Restrict user input to yes or no in PowerShell while using Read-Host

This is not much but useful tip. Sometimes, while trying to read input from the user, you would want the response in yes/no (or y/n for that matter). You would not want users trying to enter anything or pressing any key by mistake which is mistaken for either yes/no. For this, we can use below specified simple code:

$YesOrNo = Read-Host "Please enter your response (y/n)"
while("y","n" -notcontains $YesOrNo )
 $YesOrNo = Read-Host "Please enter your response (y/n)"

Above example can also be modified to get limited set of choices from users.

SQL Error : Index was outside the bounds of the Array (Microsoft.SqlServer.smo)

If while working on Azure SQL PaaS or even on-premise SQL database, you are stuck with this error “Index was outside the bounds of the Array. (Microsoft.SqlServer.smo)”, the most likely reason is that the version of SQL server management studio on your local machine is on the lower version that the SQL server version on the server end.

In such a scenario, you would need to upgrade the SSMS version installed on your local machine. Apparently, the reason behind the error message is that SQL couldn’t show new features in your old SSMS version. Read More »

Configure PowerShell prompt to always display date and time

Sometimes, we may want to know when a particular PowerShell command was executed in our current shell. Simply scrolling through the PowerShell history will not help you as it just shows the commands. Having current date and time as part of PowerShell prompt improves the situation to a greater level. It also helps you in recording your changes in proper way.

Here is a small piece of code which helps you to add current date time to PowerShell prompt. You can change the formatting of the date and time to meet your requirements. Read More »

Configure PowerShell profile before next time you run PowerShell

A PowerShell profile in very simple terms, is a script that is auto-executed (read runs) whenever you start a PowerShell session. Why is this important? Suppose, you work on SharePoint administration for your daily bread and butter. Everytime, you would want to execute some cmdlets based on SharePoint, you would need to load PSSnapin for SharePoint. Same goes for Azure. Who would want to login everytime whenever you want to run some Azure cmdlets and go through that annoying pop-up box. It’s a good idea to have some commands auto-executed whenever you want to open PowerShell session. Read More »

Enforce PreRequisites match before running PowerShell scripts

Many a times, you would write a script, have it happily running on your machine, share it with someone and get a call from them only to find that your script is not functioning! We all have ran into this situation once or more in our life. Some people would avoid it by writing lengthy documentation. But who reads documentation any more. So you would go to their desk and solve it for them. To overcome this problem, Microsoft has added #Requires statement in PowerShell.

The #Requires statement prevents a script from running unless the Windows PowerShell version, modules, snap-ins, and module and snap-in version prerequisites are met. If the prerequisites are not met, Windows PowerShell does not run the script. Read More »

Read multi line input from users in PowerShell

As you are aware, Read-Host cmdlet can be used to read single line input from users in PowerShell. But there is no out-of-the-box cmdlet to read multi line input from users. We can achieve this in a roundabout way:

 $inputFromUser = @()
 $input = ''
 While($input -ne "q") {
 If ($input -ne $null) {
 $InputFromUser += $input.Trim()
 $input = Read-Host "Enter the input here (Enter 'q' to exit) "
 $inputFromUser = $inputFromUser[1..($inputFromUser.Length-1)]

Read More »

Persistant Azure PowerShell Logins

While doing azure automation from some time, I have found that I have to login each time I run a new PowerShell session. This is irritating while writing and executing scripts. You can do this manually by configuring the management certificate and subscription details with the Set-AzureSubscription and Select-AzureSubscription cmdlets or automatically by downloading the PublishSettings file from Windows Azure and importing it. In this blog post, we’ll see how to automate the login process for Microsoft Azure so that our script can run without any manual intervention. Read More »