Category Archives: Windows

GNU utilities native in windows

For all you Linux fans out there, that use a lot of linux commands (sed,ls,awk,etc) and would like to have this also on linux, you can give UnxUtils a try. The big differences between UnxUtils and Cygwin is that UnxUtils uses executables that are only dependent to Microsoft C-runtime (msvcrt.dll).

All you have to do is:

1. Download the zip file from owners site

2. Unzip the file in any location (preferably C:\Program Files (x86)\UnxUtils )

3. Add the path C:\Program Files (x86)\UnxUtils\usr\local\wbin to the System Variables PATH (Start – Control Panel – System – Advanced system settings – Advanced – Environment Variables – System variables – PATH – edit)

You can find a complete list of programs that are ported on the UnxUtils site.

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

$path="F:\Shared"
$olderthan=14
$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.

Send email from command line

If you need to send an email through a smtp server (and you are allowed to do that) you can use the following lines after you telnet to that server:

helo mail
mail from: sender@example.com
rcpt to: recipient@example.com
data
From: "Sender" <sender@example.com>
To: "Recipient" <recipient@example.com>
Subject: Here you type your subject
Here you type your body.
.