Tag Archives: powershell

List users in an AD group

If you want to quickly list all the users in a group (and even count them) you need the DN name of the group and the following line entered in cmd or PowerShell:

dsget group "CN=Group,DC=Contoso,DC=com" -members -expand

This will list all the users/groups that are members of that group. To find out the number of members pipe the output to find using the c switch (all DN have a DC in their name):

dsget group "CN=Group,DC=Contoso,DC=com" -members -expand | find /c "DC"

Hint: If you use a special syntax for groups (group_groupname or abreviation_groupname) you can exclude all the groups (using find’s v switch) and you will find only the users in that group.

dsget group "CN=Group,DC=Contoso,DC=com" -members -expand | find /c /v "group_"

Delete files and folders older than…

Here is a little PowerShell script  that will delete all files and folders (and all the subdirectories and files) that have the created and last modified times older than $olderthan days. You just need to change the following:

$path – specify the path where the script will search.
$olderthan – number of days you want the files and folders to be kept on the server.

You can change the days in hours, minutes,months and even years. All you have to do is change “adddays” with what you need. Example: “addminutes”,”addyears”, etc.

#Delete files older then specified days
#Version 0.1
#Created by nraddan@yahoo.com

$olderdate = (get-date).adddays(-$olderthan)
$FilteredList = Get-ChildItem $path -force -recurse | where { ($_.lastwritetime -le $olderdate) -and ($_.creationtime -le 

$olderdate) }

if ($FilteredList.count -ne 0) {
#remove files older then $olderthan days
    $FilteredList | where { $_.Length -ne $null } | % {
        $creation=(get-date) - $_.creationtime
        $modified=(get-date) - $_.lastwritetime
        write-host Deleting file $_.PSPath ! "( $creation - $modified )"
        remove-item $_.PSPath -Force
#remove folders older then $olderthan days
    $FilteredList | where { $_.Length -eq $null } | % {
        $creation=(get-date) - $_.creationtime
        $modified=(get-date) - $_.lastwritetime
        write-host Deleting file $_.PSPath ! "( $creation - $modified )"
        remove-item $_.PSPath -Force

The script was test on Windows 2008 R2 Standard.