Wednesday, November 9, 2011

Use “PowerCfg.exe” to change “Power Scheme” of the system with the help of “PowerShell”


Hi,

In my office everyone is using Different “Power Schemes” on there  systems. it’s good but sometime it cause little problems sometime  Somebody complains about his/her desktop go to sleep mode after “10 Minutes” which they don't like , somebody says they “ want to hibernate their laptops after 15 Minutes of idle time.
Then i think that the best way to resolve the problem is to apply  same “Power Scheme” on all the laptops, And to implement  this I choose  “PowerShell”.

The Ideas is :

Write a Script in PowerShell , which check the Current “Power Scheme” and if the scheme is one which we want to use than it wont do anything and if user have some other power scheme applied then the script change it to our one. Then put this script in to “Login Script” so that it applied and check every time when users log-in in to his/her laptop.
To accomplish this task we have used Windows inbuilt command like utility “PowerCfg.exe”

You can download the Script from here : http://gallery.technet.microsoft.com/scriptcenter/Change-Windows-Power-Scheme-3b794e0b

Some Screenshots:
Lets Run the PowerCfg –L to check the available power schemes

PowerCfg-1

Ok.. Now check which “Power Scheme is Currently Active” using PowerCfg –GetActiveScheme  , currently “Maximum Battery Life” Power scheme is active , and in our script we define that default “Power Scheme” should be “ Energy Star”

PowerCfg-2

Let’s RUN the script and see if that works….After running the script it says that “Power Scheme” Successfully applied” ,

PowerCfg-3 

Let’s Verify it., run the PowerCfg –GetActiveScheme  again and see what we got,,, Bingo !!!! now our current  active scheme is “Energy Star” which means our script is running successfully

PowerCfg-4

You can download the Script from here http://gallery.technet.microsoft.com/scriptcenter/Change-Windows-Power-Scheme-3b794e0b

Thanks
Aman Dhally 

Tuesday, November 8, 2011

Close multiple “Not responding” Processes or Applications using “PowerShell”

 

Hi,

Yesterday I was working on Powerpoint, Word, and Excel simultaneously  and the files which i have opened in these programs was very heavy in size. so after few hours of continuous working on that these application got “Hanged” and they were "stopped Responding” .

Then I open “Task Manager”, select the each process and Kill them manually.

at that point I think, there should be some “PowerShell Way” to achieve the same task using script.

Yes there, Let me share it with you.

The cmdlets which we use to accomplish are: Get-Process , Where-Object, Get-Member (alias is gm)

Let’s Start :

Open PowerShell Console

type Get-Process and (|) Pipe to another cmdlet Get-Member or you can use the alias GM ,

Capture 0

this command shows your the all properties  and Methods used by Processes.

We are Interested the only two member properties, one is the Responding Property which in boolean (either True or False) and another one is Kill() which in Method to kill the process

Capture-0

Capture-5

lets begin.

for example :

My Notepad is stopped working and lets close it using PowerShell

Capture

type the command

Get-Process   | Where-Object { $_.responding -eq $false}

get-process will get the list of all running process and then piped to another command, the Where-Object command select only those services whose Responding status is False or we can say Not-responding

It found Notepad is not responding

Capture-2

but i want to put the above command in to variable, so if there is more then one process which are not responding they all get close in a single command.

$notres = Get-Process   | Where-Object { $_.responding -eq $false}

Capture-3

I used $notres variable and it stores our Get-Process | Where-Object { $_.responding -eq $false} command.

Now execute the Kill() Method and you will see that all “Not Responding” processes are closes now :)

$notres.Kill()

   Capture-4   

 

That’s All

 

Thanks for viewing

 

Aman Dhally

www.amandhally.net

Friday, October 28, 2011

“Windows Powershell 2.0 Bible”

 

Hi,

Today the postman delivered the two brand new copies of book  “Windows PowerShell 2.0 Bible” published by “John Wiley & Sons, Inc.” and written by “Thomas Lee”, “ Karl Mitschke” , “Mark E. Schill” and “Tome Tanasovski”.

IMG_0042

This Book send to us by “Mark E. Schill”

From the behalf of “New Delhi PowerShell user Group” Thanks a lot  “Mark E. Schill” and also thanks to  “Wiley Publications” too to send a complimentary copy of it ..

you can purchase the Book in India from flipkart: http://www.flipkart.com/books/1118021983?pid=0fx3fb371e&_l=GOondWnomHOpT1nYHiHhRg--&_r=Me02lPU3E3R%20JZUOhY7ZAA--&ref=e86a7d5b-b871-424b-9041-610e5362b060

and if you are out of India then you can purchase from Amazon: http://www.amazon.com/Windows-PowerShell-2-0-Bible-Thomas/dp/1118021983/ref=sr_1_1?ie=UTF8&qid=1319806233&sr=8-1

 

Enjoy reading :)

Thanks

Aman Dhally

Friday, October 21, 2011

How to run Powershell commands in “cmd”


Hi,
Sometime you want  to run PowerShell commands in Batch (.bat) files or may be from old command shell (cmd.exe) due to some specific needs or you may want to replace your old dos based email program with “ Send-MailMessage” , and you was thinking how to do it , let me show to you :)

comand prompt
So let’s Do it !!!!!
Step.1
Open “Command Prompt”
  • Go to “RUN” {Windows Key + R }
  • Type “CMD”
  • RUN Box
  • Now on “Command Prompt” type “Powershell.exe <cmdlet Name>”
    • For Example: “PowerShell.exe Get-Process”
Bingo!!!!!!!
It’s working perfectly J

Thanks
Aman Dhally

Thursday, October 20, 2011

Generate System Information Reports using WMI and PowerShell

 

Hi Guys,

Do you ever want to see what going on users machines? or what Settings he have on his system,?

If you have few users in your office then you can go to there desk and check it manually, but –whatif ? you have hundreds on users or you are supporting a remote users.

I just came to same problem when i want to support a remote users and he don’t know how to run “Task manager”, how to open “RUN” dialogue box so that we can run “Msconfig” to view the list of soft wares which are on system startup list , and the internet connection was very low at his end and i can’t be able to do anything remotely.

To overcome from this situation I wrote a little script which get the system info and put everything in a HTML file and when when script done it open the HTML file in Internet Explorer. Then you can ask user to send that HTML file to you by email.

This script is using “WMI” queries to find the information , using get-wmiobject command and the covert everything to HTML using “Convertto-Html” cmdlet and save the output in to a file.

 

Currently the the HTML report contains .

  1. BIOS Information
  2. Physical Disk Drives
  3. List of Network Adapters
  4. Operating System Information
  5. Logical Disk Drives
  6. IP Addresses for each Network Card
  7. List of Startup softwares
  8. Running Processes
  9. List of Services [ Whose “startup” mode is set  “Automatic” but they are currently “Stopped” for some reason]

 

you can also add more “wmi” queries in to the script. like, Battery info, CDROM Drive info, Printer info etc in to it.

I tested the script on “Windows7” and it is working perfectly.

The output of the script will be found at : C:\Users\USERNAME\Computername.html

you can download the whole script from here : http://dl.dropbox.com/u/17858935/SystemInformation.zip

Screenshot:

SystemInfo

Hope you like it

Thanks

aman dhally

Tuesday, October 18, 2011

How to run Child PowerShell Process in PoweShell

 

Hi Guys,

may be you are wondering what is its meaning, it mean how we can use PowerShell with in a PowerShell as a child process

It is very useful when we want to call a cmdlets or script within PowerShell Script.

Syntax

Syntax is Simple type PowerShell following by Curly Braces and in Curly braces you can provide any script, or cmdlet.

PowerShell  { }

for example  open PowerShell console and type the below command:

powershell {Get-ChildItem c:\}

ChildPowerShell process0

How we know that its the above command running as Child Process not as a same process.

okays I have a proof.

Currently there is  only one “PowerShell.exe ”  process is running.[fig.1]

ChildPowerShell process  fig.1

 

now execute our command as let’s see what happens [fig.2]

ChildPowerShell process 2 fig.2

now see below when we execute the script a new child “PowerShell.exe” process starts beneath our old “PowerShell.exe” process and when it done executing it the child process will exit automatically [fig.3]

ChildPowerShell process 3 fig.3

 

hope u like it :)

thanks

aman dhally

Send HTML formatted email using PowerShell


Hi Guys,
Did you ever tried to send HTML formatted colour coded email using PowerShell ? I was trying to do it but didn’t get any good and useful information on it, but i finally manage to get it done.
So lets start:
For example i am sending a test email from my official account to my GMAIL account.
 the script will look like  this:

##############  Script start Here ##########
# $smtp variable contain the name/ip of your email server ##
# $to contain the email id whom you want to send email ###
# $from contain email of sender ###
# $Subject contain subject of the email.
# In Body we are defining some HTML coding in to our email message body
# <b> means BOLD
#<br> means Break go to next Line
#<a href> provide a link to the text
# <font color=red> , give the color to the font
$smtp = "Exchange-Server"
$to = "Aman Dhally <amandhally@gmail.com>"
$from = "Aman Singh <aman.dhally@analysysmason.com>"
$subject = "This is a Test of HTML Email"

$body = "Dear <b><font color=red>$to</b></font> <br>"
$body += "We are testing <b>HTML</b> email <br>"
$body += "Click <a href=
http://www.google.com>here</a> to open google <br>"
#### Now send the email using \> Send-MailMessage
send-MailMessage -SmtpServer $smtp -To $to -From $from -Subject $subject -Body $body -BodyAsHtml -Priority high
########### End of Script################

Note: Make sure you use –BodyasHtml parameter in Send-MailMessage otherwise it send as plain text format.
Now lets see how this email will look like :)
Send-MailMessage
Cool!!! it look exactly how we have define it in our Script :)
you can find more HTML coded which you can use in script from here http://www.w3schools.com/html/default.asp

take care
aman dhally