List duplicate entries count in array using powershell

September 2nd, 2010 Sitaram Pamarthi No comments

I have a file with list of users with duplicate entries. My goal is to process this file and generate the duplicate list. I also need to know how many times a entry got repeated in file incase of duplication. This is the requirement I got today and I could able to fulfill this with less efforts using PowerShell. 

So, here is the script. 

$hash =@{}

$users = Get-Content c:\temp\userslist.txt

$users | % {$hash[$_] = $hash[$_] + 1 }

$hash.enumerator() | % { $_.value -gt 1 }

In above example, I used a hash table to keep track of number of entries of each. We can use arrays here but that won’t help you listing the duplicates count. If you would like read more details about hashes, please refer http://technet.microsoft.com/en-us/library/ee692803.aspx

Categories: PowerShell Tags: ,

Tip: Get the domain controller details using PowerShell

September 2nd, 2010 Sitaram Pamarthi No comments

 

This small powershell command helps you to get the domain controller name currently in use.

PS C:\> ([ADSI]“LDAP://RootDSE”).dnshostname
DC1.Techibee.com
PS C:\>

You can also know to which site this DC belongs to.

PS C:\> ([ADSI]“LDAP://RootDSE”).servername.tostring().split(“,”)[2].Split(“=”)[
1]
SITE1
PS C:\>

Isn’t it looking a bit complex to query the site name? Any alternatives? Yes, we do have one. Below helps you to query the Site name of DC you connected.

import-module ActiveDirectory

Get-ADDomainController -Discover | select Site

Site
—–
SITE1

Note that above command requries ActiveDirectory PowerShell module which is generally available in Windows 7. One more requirement to make ActiveDirectory Module work agaist your Active Directory is, your DCs should have ADWS(active directory web services) installed. These services comes by default with windows 2008 servers but on windows 2003 servers you need to install this additional components.

Refer to my old article about Active Directory Administrative Center where I talked about ADWS for windows 2003.

Tip: Expand Values in multi-valued property – PowerShell

August 31st, 2010 Sitaram Pamarthi No comments

Sometimes you see incomplete data in powershell output. This kind of output ends with three dots(…) which resembles some data which shell is unable to display. Below is one such example, where I am trying to view the permissions of a folder via Get-Acl

Here, “Get-Acl | select access” works but will display similar output since it has more amount of data to display which cannot be accommodated into shell window. In such cases, to see the complete output, we can use a parameter named “-expandproperty”. This prints the complete value in shell.  Below is the example.

Get-ACL | select -expandproperty access

Hope this helps…

Categories: PowerShell Tags: , ,