Prof. Powershell

The PowerShell File Frontier, Part 1

Here's how to grab files from a different computer, even if they're not synced.

In my day-to-day work, I use a variety of cloud based services to keep a variety of folders synchronized between the different computers I use. But occasionally I need to grab some files from a nonsynchronized computer and copy them to one of my synchronized devices. Use PowerShell, this is a pretty straightforward task. In fact, I bet if you have to manage files for people, or even for yourself, you might find these techniques useful, especially if you are still learning PowerShell.

In PowerShell you can use the dir command, for the most part just as you would in the CMD shell. Bear in mind the dir command is actually an alias for the Get-ChildItem cmdlet. Starting in PowerShell 3, I believe, Get-ChildItem introduced dynamic parameters when used in a filesystem PSDrive. If you look at help when your prompt is set to the C:\ drive you will see parameters for –File and –Hidden among others. If you change to say the CERT: PSDrive and look at help again, you won't see those parameters. As long as the path you are using with Get-ChildItem is a filesystem path, you can use these parameters:

PS CERT:\> dir c:\work -directory

Because Get-ChildItem by default returns both directory and file objects, these dynamic parameters can come in quite handy. There are also some great performance gains. When I measure this command:

dir c:\scripts -recurse | where {$_.psIsContainer}

It took 272 milliseconds. But let's try using the –Directory dynamic parameter:

dir c:\scripts -directory -recurse

This only took 42 milliseconds. This was only for 55 directories but I think you get the idea. With that, let's get back to my original need and that is to copy some files.

The file object you get with Get-ChildItem has a property called LastWriteTime which is a DateTime object indicating when the file was last changed. If you look at a file object with Get-Member you will also see a LastAccess time but I never trust that as an indicator of when a real person may have touched a file. This property could be updated by software such as anti-virus or other scanners. So all I need to do is get all the files from the source location that have changed since a given time and copy them to my target computers.

PS C:\> dir \\jdhlap01\c$\scripts -file | where {$_.lastwritetime -ge '8/1/2014'} | copy –Destination C:\Scripts –passthru

This will copy any file from C:\Scripts on JDHLAP01 that was modified on or after August 1, 2014 and copy the file to the local C:\Scripts folder. It really is that simple. Next time we'll look at some other options.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular