Thursday, March 7, 2013

make mine a double

using the copy command

when managing a windows server i sometimes find myself needing to copy many files. for the sake of efficiency i head for the command line. the copy command can be used to quickly make a copy of a file:

C:\>copy q1report.txt q2report.txt

this copy command takes two arguments. the first is the file to copy, the second is the name of the new file to create. when you just want to make a copy of a file in a new location, use the destination directory as the second argument:

C:\>copy q1report.txt reports

this will make a copy of q1report.txt in the c:\reports directory. you can copy multiple files at once using wildcards:

C:\>copy q*report.txt reports

retaining permissions with xcopy

when you copy a file only the contents are duplicated. a file's permissions don't copy with it, but the duplicate inherits the permissions of the directory where it gets created. if you want to copy both the file and its access control list, you'll need to use xcopy:

C:\>xcopy /o q1report.txt reports

xcopy can also be used to copy whole directory trees:

C:\>xcopy /s reports d:\

this command will copy the entire directory structure under c:\reports to drive d.

concatenating files with copy

when you need to combine several files into one the copy command can be used for concatenation. just join all the old filenames together with plus symbols and specify the new filename.

C:\>copy q1report.txt+q2report.txt+q3report.txt+q4report.txt annual_report.txt

this works great for combining ascii text files together, but it's not generally very useful with binary files like those created by a word processor.

copy as a text editor

the copy command can also be used as a quick-and-dirty plain text editor. start with the command copy con filename and then type the lines you want in your file. when you're done, type ctrl-z.

C:\>copy con ls.bat
@echo off
dir
^Z

now your batch file is saved and ready to rock 'n' roll.

for more advanced features, check out the command line help for copy and xcopy, and, as always, be seeing you.

Monday, February 25, 2013

wish (i knew when) you were here

User accounts in Active Directory store information about the last time a user was logged in. How can we use PowerShell to get the number of days since a user last logged on to the domain? A simple way to do this is to use DateTime objects. These guys have some nifty methods that deal very effectively with operations on—well—dates and times.
So what we’re going to do is get a user object’s last logon as a DateTime. The property we’re looking for is
the LastLogonDate property. We’ll also get the current time, and use the DateTime object’s Subtract method to determine the difference between then and now.
$DavesLastLogon = Get-ADUser Dave -Property LastLogonDate | 
        Select-Object -ExpandProperty LastLogonDate
$Now = Get-Date
$DaysSinceLastLogon =  $Now.Subtract($DavesLastLogon).Days
The resulting TimeSpan object has a Days property that we can examine and, voilĂ , that’s how many days have gone by since the user last logged on.
Use the object’s ToString method to get the days, hours, minutes and seconds in a format suitable for reports:
$Now.Subtract($DavesLastLogon).ToString()
Be seeing you.
​