« Posts tagged powershell

Quickly rename a large number of files with PowerShell and Regular Expressions

There have been a few times in the past where I’ve had to rename a large number of files for various reasons (ie: remove a common piece of text from the name) and I’ve always resorted to PowerShell.

Piping dir into a where and matching the files I wanted to rename was effective but tedious. Cue the mass_rename.ps1 script:

$ext = $args[0];
$dir = $args[1];

$what = $args[2];
$with = $args[3];

$whatif = $args[4];

$count = 0;

if ($args.length -lt 4) {
    write-host "Invalid parameters" -fore red;
    ""
    write-host "   .\mass_rename.ps1 <ext> <dir> <what> <with> [-whatif]";
    ""
    write-host " Example (don't do any replacing, -whatif):";
    write-host "   .\mass_rename.ps1 .docx c:\Documents 'version 1\.1' 'version 1.2' -whatif";
    ""
    exit 1;
}

ls -recurse -path $dir | ?{ ($_.name.endswith($ext)) -and ($_.name -imatch $what) } | %{
    if ($whatif -eq "-whatif") {
        write-host("whatif: '" + $_.fullname + "' -> '" + ($_.name -ireplace $what,$with) + "'");
    }
    else {
        $from = $_.fullname;
        $to = ($_.name -ireplace $what,$with);
        mv -literalpath $from -destination ($_.directoryname + "\" + $to) -force;
        write-host "Renamed '$from' -> '$to'" -fore yellow;
        $count++;
    }
}

write-host "Done. Processed $count files." -fore green

The script will accept 4 parameters with an optional -whatif as the 5th. Fairly self explanatory with one mention: the <what> parameter is a regular expression. Keep this in mind when, for example, you are trying to match for a period (.) as you would have to escape it (as per the example usage).

The -whatif parameter will only output the before and after file names thout modifying the files themselves.

That’s it, set the execution policy and enjoy.

Exchange 2007 SMTP Send Connector on a port other than 25

I’ve setup an Exchange 2007 SP1 server recently to sync with a few remote Exim POP/IMAP accounts, in order to provide push email to my new Windows Phone 7. After battling with certificate issues, I managed to sync the phone to the Exchange server with all of the accounts I wanted. Email was being pushed, all was well. The only issue was.. sending email using the ActiveSync accounts on WP7.

By default Exchange 2007 does not relay messages to remote domains (@gmail.com, etc). You have to create a SMTP Send Connector for all domains (*, or specific domains if you wish) on the Hub Transport Role server. This is all well and good, however, my ISP blocks all outgoing connections to port 25. If you’re using the GUI to create this connector you wont have the option to modify this port number.

Say my connector name was “All Mail” I would type this in the Exchange Management Shell:

Set-SendConnector -id "All Email" -port 2525

And that’s that. Exchange can talk to my Exim server and relay messages to the outside world.

To see the current port assigned to the “All Email” connector type in the Exchange Management Shell:

(Get-SendConnector -id "All Email").Port