Wednesday, August 19, 2015

PowerShell Tips : Check if you your PowerShell command runs fine or not.

 

Sometimes, we need to know, if our PowerShell command run successfully or not.

Normally if there are any errors, we see them in red. But still!!

There is a special variable in PowerShell, Dollar sign and question mark $?, which tell about , if the last command runs successfully or not.  If the command runs successfully, the output is set to true, and it the command doesn't run successfully, the output contains False.

$?

12

Now, you must be thinking, what will happen, if the -ErrorAction is set to SilentlyContinue. And you are not able to see the red-error code, in-those-scenario $? is really helpful to know , if the command runs fine or not.

1234

 

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”.

Tuesday, August 18, 2015

PowerShell Tips : Find Hard Disk Drives and USB Disk Drives using PowerShell.

 

Do you ever eager to know, how may Disk drives are attached to your system by using PowerShell? if yes! then you must try the Get-Disk cmdlet.

When you run the Get-Disk cmdlet, it shows you list of all list drives you have attached to the system.

And! if you eager to know , that, how many USB disk drives are attached to your system, then you, can select to show the BusType equal to USB and it will show you only the list of USB hard disk drives attached to your laptop.

Get-disk

Get-disk |  where { $_.bustype -eq "usb" }

 

Aman Dhally - Manya Kaur

 

Regards

dove

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”.

 


Thursday, August 13, 2015

PowerShell Tip : Logging time taken by a PowerShell script to complete.

 

Do you wonder sometime, that how much time you script it taking to complete, or, when you have started the script and then it finished.

I love to see it, and I use a very simple  way to monitor it.

The trick is to create a variable in the first live on the script and save the Get-Date info, and in the end of the script create another variable and save the same Get-Date information to it , and then minus the start date variable with the end date and Voila! you will get the information.

  $startTime = Get-date

 Get-ChildItem c:\

 $endTime = Get-Date

 $endTime - $startTime

 

Aman Dhally - Manya Kaur

 

Aman Dhally - Manya Kaur

Thanks for your time.

Animated_peace_on_earth_hg_blk

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”.