Monday, December 15, 2014

PowerShell Tips : Setting Requirements using #Requires.

 

This is a very cool PowerShell Tip and I am sure that you are going to love it.

There are few script which you want to Run as Administrator and you want to set dependency that the script should only works if the PowerShell console is running in a elevated /administrator mode, other wise  it should not run.

How you are going to  set this dependency? think think ... using a function? that will be complicated.. then ?

Ok!! the answer is , using #Requires

In the top of of your script, type :

#Requires -RunAsAdministrator

That's all. save the script and then try to run the script in admin mode and in normal mode and you can see the difference.

Let's see the example.

In the below script, I have added only two lines of code. , in first line we set the Dependency using #Require to execute the script only if it is running as admin.  and in the next line, we are just using the Write-Output cmdlet to write the text  "I am running as Administrator"

Let's save the script and run it.

12-12-2014 11-42-12 

I am running the script in normal windows  PowerShell console session,and you can see that it throw the error.

12-12-2014 11-40-45

Now, let's run the script in elevated mode, and you can see below, this time it run as expected, without throwing any error.

12-12-2014 11-41-29

 

12-12-2014 11-41-49

Video :  You can see the video of it.

Setting Requirements using #Requires.
There is lot's more which we can achieve using #Require. To know more about #Requires read the help
PS C:\> Get-Help About_Requires
15-12-2014 17-16-42
I have tested this in PowerShell V4.0.
Thanks for reading the blog and viewing the video.
Regards.
Aman Dhally
If you like, you can follow me on Twitter and Facebook. You can also check my “You Tube channel for PowerShell video tutorials. You can download all of my scripts from “Microsoft TechNet Gallery”.

Murky_ana

Thursday, December 4, 2014

PowerShell Tips : Using CTRL + END Key Combination.

 

Picture1

Today’s a PowerShell Console TIP is to use the CTRL + END Key Combination. Actually these key Combinations works of DOS Console too. So these are kind of Console shortcuts.

We can use the CTRL + END key combination to remove the all text after the Cursor.

Cool! No? try it now.

Type any Long PowerShell command and bring the cursor in the middle of the text and press CTRL + END  and voila see the magic.

Please see the below video about it. Smile 

Using CRTL + END Key.

Thanks for viewing.

Aman Dhally

If you like, you can follow me on Twitter and Facebook. You can also check my “You Tube channel for PowerShell video tutorials. You can download all of my scripts from “Microsoft TechNet Gallery”.

 


Murky_ana

Tuesday, December 2, 2014

PowerShell Tips : The F7 key.

 

Hello, you must be wondering where I have been from past few months?

I was busy with some of my life’s awesomeness. Hopefully I will blog about that soon too.

Let’s talk about PowerShell, as we spent lots of our time on PowerShell console and the knowledge of some handy “Keyboard Shortcuts & Tricks” are always helpful and time savers.

If you are using PowerShell from long time, you may be already knew these shortcuts, if not, then at-least you know them now.

F-7 Key

On PowerShell Console, if you want to see the history of the cmdlet’s or commands which you have run, and you want to re-run again, then hit the F7 key of your keyboard, and you will see the list of cmdlet and command which you have run on the PowerShell console.

1

Cool ! Isn’t.

 

PowerShell TIPs

Thanks.


 

Aman Dhally

If you like, you can follow me on Twitter and Facebook. You can also check my “You Tube channel for PowerShell video tutorials. You can download all of my scripts from “Microsoft TechNet Gallery”.

 


Murky_ana