Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Powershell: set .msg file date to date the mail was created.

$
0
0
Kind people.
I've looked here: 
And here:
What I want is to drop & drag a message from Outlook to the filesystem.
Next a powershell script which changes the creationdate of the file (in the explorer shown in the column [Date modified])
to the date when the mail was made.
I pinched the code from:
Which in itself works well.
And turned it into this:
$olMailItemPath = "C:\msg\*"

  Write-Host $olMailItemPath
$x =0
$SourceFolders = Get-Item $olMailItemPath
echo $SourceFolders . count
$outlook = New-Object -comobject outlook.application
$namespace = $outlook. GetNamespace( "MAPI")
 
foreach ($_ in $SourceFolders )
    {
    $SourceFolder = $_
    Write-Host "SourceFolder is $SourceFolder "
    $SourceFiles = Get-ChildItem -path $SourceFolder -recurse -include *.msg    
    $SFCount = $SourceFiles. count
    Write-Host "Source File Count is $SFCount "
    $objDraftFolder = $outlook. Session .GetDefaultFolder( $olFolderDrafts )
    $objDeletedFolder = $outlook. Session .GetDefaultFolder( $olFolderDeletedItems )
    $colItems = $objDraftFolder. Items
    $FolderItemCount = $colItems. Count

    foreach ($_ in $SourceFiles )
        {
        $x ++
         Write-Host $x
        $MailItem = $_
         Write-Host "Mail Item is $MailItem "
        $olMailItem = $NameSpace. OpenSharedItem( $MailItem)
        $DateRecieved = $olMailItem. ReceivedTime
         Write-Host "Date Recieved is $DateRecieved "
        $Mailitem .CreationTime = $DateRecieved    # To show up in explorer (in column Date modified)
        }
    }
 

Now wat bothers me is the last three lines i added.
The last line invariable gives me: 
Exception setting "CreationTime": "The process cannot access the file 'C:\msg\concrats.msg' because it is being used by another process." 
This stomps me. Please help.
Kind regards, Ad Weterings.



 

 


WHS owner


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>