3

I'm iterating through all files of a document library using powershell trying to update some files in some folders. I can read the file properties but I'm not able to update them.

Here is my code:

foreach($list in $web.Lists) {

    if ($list.Title -ne "My Documents") {
        continue
    }

    Write-Host $list.Title

    foreach($folder in $list.RootFolder.SubFolders) {

        if ($folder.Name -ne "My Folder") {
            continue
        }

        Write-Host $folder.Name -ForegroundColor Gray

        foreach($file in $folder.Files) {

            # field type: users and groups
            Write-Host $file.Properties['My Cust Prop 1']
            # output: 40

            # field type: date and time
            Write-Host $file.Properties['My Cust Prop 2']
            # output: 2012-07-11T00:00:00Z

            #This does not work:
            #$file.CheckOut();
            #$file.Properties['My Cust Prop 1'] = 30
            #$file.Properties['My Cust Prop 2'] = '2012-10-25T00:00:00Z'
            #$file.CheckIn('Test');

        }

    }

}

What am I doing wrong? How do I have to change the code to make this work?

How can I get the User ID if I only have the AD account?

4

This should work, you need to call Update method before Checking In

$file.CheckOut();
$file.Properties['My Cust Prop 1'] = 30
$file.Properties['My Cust Prop 2'] = '2012-10-25T00:00:00Z'
$file.Update();
$file.CheckIn('Test');

Or you can try

$listItem = $web.GetListItem($file.ServerRelativeUrl);
$listItem.File.Checkout();
$listItem.Properties['My Cust Prop 1'] = 30
$listItem.Properties['My Cust Prop 2'] = '2012-10-25T00:00:00Z'
$listItem.Update();
$listItem.File.Checkin('Updated')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.