Freigeben über


Changing file dates and other more uncommon properties in PowerShell

There are some file properties that aren’t too easy to change. Neither in the command line or in Windows Explorer. One that I’ve wanted to change personally quite often is the created and changed dates of a specific file, or a batch of files.

Background

I frequently use my Windows Phone (currently a Nokia Lumia 800) for listening to audio books. I’ve found that the best way to do so is to change the category of the files to “podcast” and they’ll show up as podcasts and I won’t have to keep track of where I was, etc. since Zune will do that for me. Zune uses the creation date to order the files in a podcast, and some times the creation dates of my files may not be entirely correct. There are a lot of shareware programs that will allow you to do batch updates on files and change these properties, but it’s actually a lot easier to do it yourself in PowerShell

How to fix this

If you have the files sorted the way you want alphabetically and want to order them by creation date in the same way, then this is a pretty easy way to do it.

$files = dir
for($i=0;$i -le $files.GetUpperBound(0); $i++)
{
   ($files[$i]).CreationTime = (get-date).AddSeconds($i)
   ($files[$i]).LastWriteTime = (get-date).AddSeconds($i)
}

Naturally you could alter other properties as well simply pass one of the objects into get-method to see what’s available:

Name                      MemberType     Definition
----                      ----------     ----------
Mode                      CodeProperty   System.String Mode{get=Mode;}
Create                    Method         System.Void Create(System.Security.AccessControl.DirectorySecurity director...
CreateObjRef              Method         System.Runtime.Remoting.ObjRef CreateObjRef(type requestedType)
CreateSubdirectory        Method         System.IO.DirectoryInfo CreateSubdirectory(string path), System.IO.Director...
Delete                    Method         System.Void Delete(), System.Void Delete(bool recursive)
Equals                    Method         bool Equals(System.Object obj)
GetAccessControl          Method         System.Security.AccessControl.DirectorySecurity GetAccessControl(), System....
GetDirectories            Method         System.IO.DirectoryInfo[] GetDirectories(string searchPattern), System.IO.D...
GetFiles                  Method         System.IO.FileInfo[] GetFiles(string searchPattern), System.IO.FileInfo[] G...
GetFileSystemInfos        Method         System.IO.FileSystemInfo[] GetFileSystemInfos(string searchPattern), System...
GetHashCode               Method         int GetHashCode()
GetLifetimeService        Method         System.Object GetLifetimeService()
GetObjectData             Method         System.Void GetObjectData(System.Runtime.Serialization.SerializationInfo in...
GetType                   Method         type GetType()
InitializeLifetimeService Method         System.Object InitializeLifetimeService()
MoveTo                    Method         System.Void MoveTo(string destDirName)
Refresh                   Method         System.Void Refresh()
SetAccessControl          Method         System.Void SetAccessControl(System.Security.AccessControl.DirectorySecurit...
ToString                  Method         string ToString()
PSChildName               NoteProperty   System.String PSChildName=.gimp-2.6
PSDrive                   NoteProperty   System.Management.Automation.PSDriveInfo PSDrive=C
PSIsContainer             NoteProperty   System.Boolean PSIsContainer=True
PSParentPath              NoteProperty   System.String PSParentPath=Microsoft.PowerShell.Core\FileSystem::C:\Users\j...
PSPath                    NoteProperty   System.String PSPath=Microsoft.PowerShell.Core\FileSystem::C:\Users\jstraar...
PSProvider                NoteProperty   System.Management.Automation.ProviderInfo PSProvider=Microsoft.PowerShell.C...
Attributes                Property       System.IO.FileAttributes Attributes {get;set;}
CreationTime              Property       System.DateTime CreationTime {get;set;}
CreationTimeUtc           Property       System.DateTime CreationTimeUtc {get;set;}
Exists                    Property       System.Boolean Exists {get;}
Extension                 Property       System.String Extension {get;}
FullName                  Property       System.String FullName {get;}
LastAccessTime            Property       System.DateTime LastAccessTime {get;set;}
LastAccessTimeUtc         Property       System.DateTime LastAccessTimeUtc {get;set;}
LastWriteTime             Property       System.DateTime LastWriteTime {get;set;}
LastWriteTimeUtc          Property       System.DateTime LastWriteTimeUtc {get;set;}
Name                      Property       System.String Name {get;}
Parent                    Property       System.IO.DirectoryInfo Parent {get;}
Root                      Property       System.IO.DirectoryInfo Root {get;}
BaseName                  ScriptProperty System.Object BaseName {get=$this.Name;}

Well, that’s basically it. Enjoy!

/ Johan