4

I am sure this might have been asked a million times before. I am very new to Power Shell and would like to ask if I am doing this right.

In the directory, we have many files types. What I am trying to accomplish is to move only PDF files that are older than one month. To not even touch the other file extensions. The extensions in the folder are:

pdf, xml, csv

I have searched the forums prior to asking. This is what I have so far.

get-childitem -path \\server\folder -include "*.pdf" -exclude "*.xml,*.csv" | where-object {$_.LastWriteTime -gt (get-date).AddDays(-31)} | move-item -destination \\server\folder\folder2

One question though, how would you handle an exclude if there is no file extension?

Thanks for your time and patience with this noob!

user3013729
  • 65
  • 2
  • 3
  • 7

4 Answers4

3

There is no need to use "where" to test the extension as get-childitem does this for you. Although I would use the filter parameter (2nd positional parameter) in the case of a single extension to search for e.g.:

$date = (get-date).AddDays(-31)
get-childitem \\server\folder *.pdf | where-object {$_.LastWriteTime -gt $date} | 
    move-item -destination \\server\folder\folder2

Btw using the filter parameter is also faster which maybe important when searching a network share.

Keith Hill
  • 194,368
  • 42
  • 353
  • 369
  • I saw that online about the -filter switch. Power Shell is a lot better than DOS! I am re-writing all our DOS scripts into Power Shell. Power Shell seems to be a lot easier for me. Thanks for the help. – user3013729 Nov 27 '13 at 21:17
  • 1
    This script seems to take every file modified within the last 31 says instead of before. – Nichlas H. Oct 15 '18 at 07:03
1

One way is to add $_.Extension -eq ".pdf" to your where-object block so that you only grab those extensions.

get-childitem -path \\server\folder  | where-object {
$_.extension -eq ".pdf" -and ($_.LastWriteTime -gt (get-date).AddDays(-31))} | move-item -destination C:\test\test

Also, if you want files older than one month, your date comparison needs to be -lt and not -gt

get-childitem -path \\server\folder  | where-object {
$_.extension -eq ".pdf" -and ($_.LastWriteTime -lt (get-date).AddDays(-31))} | move-item -destination C:\test\test
malexander
  • 4,522
  • 1
  • 31
  • 37
  • I understand that -gt is greater than? what does -lt mean? thanks for the extension var. didnt know about it. I appreciate your help. – user3013729 Nov 27 '13 at 15:53
  • No problem. -lt is "Less Than". The older a timestamp, the less it is. So you want files that are less than a date 31 days ago. Using -gt here will only grab files that have been written in the last 31 days. – malexander Nov 27 '13 at 16:00
1

Another way to do this is to specify the -Name flag and use wildcard for filename:

get-childitem -path \\server\folder -name "*.pdf" | where-object {$_.LastWriteTime -gt (get-date).AddDays(-31)} | move-item -destination \\server\folder\folder2
0

I am not sure to understand your last question, but if you have pdf files with no extension, see this discussion.

I think something like that can help you. The file.exe (download) showing the mime-type of the files, which is something like PDF document, version 1.6, depending the pdf file version.

 gci . | ? { (file.exe -b $_) -match "pdf" }
Community
  • 1
  • 1
plunkets
  • 510
  • 1
  • 6
  • 13