Tuesday, December 13, 2011

Writing Registry Key Default Values

 

PowerTip of the Day, from PowerShell.com:

 

If you need to set the default value for a Registry Key, you can  use either of these approaches:

Set-ItemProperty -Path HKCU:\Software\Somekey -Name ‘(Default)’ -Value MyValue

Or, you can just do this:

Set-Item -Path HKCU:\Software\Somekey -Value MyValue

 

thanks

Asking for Credentials

 

PowerTip of the Day, from PowerShell.com:

When you write functions that accept credentials as parameters, add a transformation attribute! This way, the user can either submit a credential object (for example, a credential supplied from Get-Credential), or simply a user name as string. The transformation attribute will then convert this string automagically into a credential and ask for a password.

function do-something {

param(

[System.Management.Automation.Credential()]

$Credential

)

'$Credential now holds a valid credential object'

$Credential

}

When you run do-something without parameters, it will automatically invoke the credentials dialog.

How to List Registry Hives

 

PowerTip of the Day, from PowerShell.com:

Use the provider name instead of a drive name when you need to get a list of all Registry Hives:

Dir Registry::

thanks

aman

My PowerShell Story: PowerShell and I

 

hi,

Thanks to http://www.powershellmagazine.com they just upload my powershell story yesterday.

13-12-2011 18-47-14

you can read the complete story here:  http://www.powershellmagazine.com/2011/12/12/powershell-and-i/

thanks

aman

Friday, December 2, 2011

Print All PDF Files in Folders using PowerShell

 

PowerTip of the Day, from PowerShell.com:

Try this one-liner if you need to print out all PDF documents you have stored in one folder:

Dir c:\myfolder\*.pdf | Foreach-Object { Start-Process -FilePath $_.FullName – Verb Print }

 


Thanks to www.powershell.com

Determining Service Start Modes using PowerShell

 

PowerTip of the Day, from PowerShell.com:

 

By using WMI, you can enumerate the start mode that you want your services to use. To get a list of all services, try this:

Get-WMIObject Win32_Service | Select-Object Name, StartMode

If you want to find out the start mode of one specific service, try this instead:

([wmi]'Win32_Service.Name="Spooler"').StartMode

Thanks to www.powershell.com
 

Change Service Start Mode using PowerShell

 

PowerTip of the Day, from PowerShell.com:

 

You can use WMI like this if you want to change a service start mode:

([wmi]'Win32_Service.Name="Spooler"').ChangeStartMode('Automatic').ReturnValue
([wmi]'Win32_Service.Name="Spooler"').ChangeStartMode('Manual').ReturnValue
 
Note that a return value of 0 indicates success. 
You will need Administrator privileges to change the start mode.
Thanks to www.powershell.com

“Enable or Disable” Network adapters using Powershell.

 

Hi,

In our IT environment we don’t give  “admin” rights to the normal users and they can’t enable/disable, install/un-install anything.

Today one of our users was having some problem with Wi-FI and to troubleshoot it i need to “enable” and “disable” Wi-Fi network adapter few times, but to enable or disable the network adapter i required to insert the “Administrator” username and password every time {remember my user don’t have admin privileges}.

network_1

This make me mad, i Inserted the username and password approx 10 times {somehow the problem was not resolved and with every change i need to disable and re-enable the wi-fi}, and I also don't want to login as administrator  to the system. Then i thought there must be a PowerShell way to do this and after few minutes of R&D i found the solution of my problem.

Problem:

user don’t have admin privileges to enable/disable any network adapters, so while troubleshooting if you need to enable or disable the network adapter it always ask for Administrator account to do the task.

 

network_1  network_2

Solution :

Run PowerShell as Administrator First

Search for PowerShell and then Right click and choose “Run as administrator

network_3

When PowerShell runs as Administrator, the PS path changed to “C:\widows\System32” and in title bar you can see the “Administrator” is written.

network_4

We will be using  WMI to enable and disable the network adapters.

I do remember that the WMI class for network adapter is start with win32_Network but i forget the full name of the class. lets search it first

  1: Get-WmiObject -List | where  { $_.Name -like "win32_network*"}

in above command we are listing all class in WMI and choosing those to display which starts with Win32_Network


It shows the all classes those are start with Win32_Network , the class which we need is Win32_NetworkAdapter


network_5


ok, lets query the Win32_NetworkAdapter class

  1: Get-WmiObject -Class win32_NetworkAdapter

and you see it shows the list of all network adapter installed in the system.


network_6 


lets filter it more and choose only Name of Adapter to display

  1: Get-WmiObject -Class win32_NetworkAdapter | select name

we piped the command further to Select-Object cmdlet and choose only to display name of the Network adpaters


network_7


Currently I am interested in enable/disable my Wi-Fi network Card. the Name of the  my Wi-Fi network adapter is “Intel(R) Centrino(R) Ultimate-N 6300 AGN


Now I will select only my “Intel(R) Centrino(R) Ultimate-N 6300 AGN” network adapter using where-object cmdlet and in $_.Name i am searching for that Name is Like something “ultimate-n”

  1: get-WmiObject win32_networkadapter |where { $_.name -like "*ultimate-n*"}

Now it showing only one Network Adapter and its showing the network adapter which i want to disable/enable. Our command is working fine till now.


network_8


Now I am going to put the above command in to a variable $wifi.

  1: $wifi = get-WmiObject win32_networkadapter |where { $_.name -like "*ultimate-n*"}

and lets also test the variable too.


All working fine ….


network_9


now pipe the $wifi to get-member cmdlet and check what the methods do we have

  1: $wifi | Get-Member -MemberType Method

NetworkAdapter has 4 methods but we are interested in only 2 for now, the enable and disable method.


To disable a Network adapter we need to use disable()


To enable a Network adapter we need to use enable()


network_10


lets try :)


first Disable it

$wifi.disable()

working


network_11


not enable it

  1: $wifi.enable()

it enabled :)


network_12


Now we can enable/disable network adapters number of times without inserting “Administrator” username and password again and again.


I hope it save someone's time :)


Thanks


Aman Dhally