WPF & PowerShell - Exception setting "Name": " is not a valid value for property 'Name'."

sigfried 81 Reputation points
2021-04-03T17:38:57.287+00:00

Hello, I'm trying to build an ADUC browser by using WPF and PowerShell but I'm stuck at a WPF peculiarity by which its naming convention does not consider spaces inside an element's name. Some OUs in AD have names with at least two words and spaces between them.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,671 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 112.1K Reputation points
    2021-04-03T20:03:44.96+00:00

    Spaces are not allowed in other projects too: WPF, Forms. You can replace spaces with underscore, probably like this (in several places):

    $itemName = $item.Name.Trim().Replace( " ", "_" )
    

    Also make sure that the name contains only letters, digits, and underscore, and starts with letter or underscore.

    Maybe you can also leave the Name of treeview item unassigned.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. sigfried 81 Reputation points
    2021-04-03T17:53:40.56+00:00

    or if anybody knows of a sample of WPF/PowerShell please let me know. I can't use C# as per requirements I have for the project.

    0 comments No comments

  2. sigfried 81 Reputation points
    2021-04-04T08:33:20.403+00:00

    Many thanks! As said I already did the trim to no avail. What did the trick was actually to leave the $item name unassigned!

    Cheers man!

    0 comments No comments