26 August 2012

WMI Query


PowerShell v3:

Get-CimInstance -Query "SELECT * FROM Win32_Process WHERE Name Like 'power%'"

Get-CimInstance -Class Win32_Process -Filter "Name Like 'power%'"


PowerShell v2:

Get-WmiObject -Query "SELECT * FROM Win32_Process WHERE Name Like 'power%'"


for more information:

http://msdn.microsoft.com/en-us/library/ms186146(v=vs.80).aspx


23 August 2012

Last logon date and hour


net user username /domain


The request will be processed at a domain controller for domain domainname.

Account active               Yes
Account expires              Never
Password last set            8/13/2012 9:13:14 AM
Password expires            10/12/2012 9:13:14 AM
Password changeable      8/13/2012 9:13:14 AM
Last logon                       8/23/2012 12:28:36 PM
Group Memberships    

The command completed successfully.

16 August 2012

Find process owner

In order to find a process owner you must use WMI;

(Get-WmiObject -Class win32_process |Where-Object {$_.name -eq 'powershell.exe'}).getowner().user

to retrive all processes owned by a user:

Get-WmiObject -Class win32_process | Where-Object {$_.getowner().user -eq 'username'}

Check “Manager can update membership list” checkbox for AD groups

If you need to give a user permission to update active directory security or distribution group members you need to give the user write permission on the active directory group object;

This can be done using the Add-ADPermission cmdlet which is availbale only on exchange management shell (it is not included in ActiveDirectory module):


Add-ADPermission -Identity ‘AD_Group_Name’ -User ‘AD_Username’ -AccessRights WriteProperty -Properties “Member”

13 August 2012

03 August 2012

Password Never Expires

If you need to enable or disable the "PasswordNeverExpires" for active directory users you can use the command:

Set-ADUser -Identity SamAccountName -PasswordNeverExpires $false


01 August 2012

Difference between single quote and double quote

 ' vs "
It is a known best practice in powershell to use double quotes only when is necessary;
Only between double quotes powershell will:

  • look for the backtick ` excape character and interpret the sequence properly;
    Example: `t will insert a tab; `n will insert new line;
  • look for the dollar $ character and interpret what follows as the name of a variable and replace the variable name with the variable content.
    Example: if $cars = 10;
    "number of cars is $cars" became: "number of cars is 10" 
These rules don't apply to single quotes and this is why is a best practice to only use double quotes only when needed;