I am trying to adapt the script found [here](https://sharepoint.stackexchange.com/a/116360/1806) to a PowerShell CSOM equivalent.

> 
> 
> ```
> function setFieldVisibility(listTitle,fieldName,properties,success,failure)
> {
>      var ctx = SP.ClientContext.get_current(); 
>      var web = ctx.get_web(); 
>      var list = web.get_lists().getByTitle(listTitle);
>      var field = list.get_fields().getByTitle(fieldName);
>      field.setShowInDisplayForm(properties.ShowInDisplayForm);
>      field.setShowInNewForm(properties.ShowInNewForm);
>      field.setShowInEditForm(properties.ShowInEditForm);
>      field.set_hidden(properties.Hidden);
>      field.update();
>      ctx.executeQueryAsync(success,failure);
> }
> 
> ```
> 
> 

In the process of generalizing it, I am trying to pass in a hashtable. I would like to iterate over this hashtable to dynamically build the properties to edit, but I'm having a little trouble. My function and usage looks like this:

```
Function Set-FieldProperties{

    param(
        [Parameter(Mandatory=$true)][string]$Url,
        [Parameter(Mandatory=$true)][string]$ListTitle,
        [Parameter(Mandatory=$true)][string]$FieldName,
        [Parameter(Mandatory=$true)][hashtable]$Properties
    )
    begin {
        $context = New-Object Microsoft.SharePoint.Client.ClientContext($Url) 
        $context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($credential.UserName, $credential.Password)
    }
    process{

        $web = $context.Web
        $list = $web.Lists.GetByTitle($ListTitle)
        $field = $list.Fields.GetByTitle($FieldName)
        foreach($key in $Properties.Keys){
            $key
            $Properties[$key]
            #failing to make this part dynamic
            $field.$key($Properties[$key])

        }
        <#$field.ShowInDisplayForm($properties.ShowInDisplayForm)
        $field.ShowInNewForm($properties.ShowInNewForm)
        $field.ShowInEditForm($properties.ShowInEditForm)
        $field.Hidden($properties.Hidden)
        try{
            $field.update()
            $context.executeQuery()
            Write-Host -ForegroundColor Green "Field properties updated successfully"
        }
        catch{
            Write-Host -ForegroundColor Red $_.Exception.Message
        }
        #>
    }
    end{
     $context.Dispose()
    }

}
Set-FieldPoperties -Url "https://tenant.sharepoint.com/teams/eric" -ListTitle "CalcColumns" -FieldName "Title" -Properties @{"SetShowInDisplayForm"=$true; "SetShowInEditForm"=$false}

```

My struggle is in how to make this portion build up the desired output:

```
 foreach($key in $Properties.Keys){
                $key
                $Properties[$key]
                #failing to make this part dynamic
                $field.$key($Properties[$key])

            }

```

How do you use the key as a property to set on the $field object in this case? Is that possible? I don't want to have some huge if block checking for all the possible properties, I'd like it to build those based on what the user passes in the $Properties hashtable.