Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1043189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:37:43+00:00 2026-05-16T15:37:43+00:00

I define a custom PowerShell type with New-Object. I would like a parameter to

  • 0

I define a custom PowerShell type with New-Object. I would like a parameter to be of my defined type, is it possible to specify this type in a declarative way? The following code gives me the error: “Unable to find type [BuildActionContext]: make sure that the assembly containing this type is loaded.”

Can we specify the type declarative, or should I just test the type of the specified object?

Not working code:

$buildActionContext = New-Object -TypeName PSObject -Property @{
# Given properties
BuildAction = "Build"; 
}
$buildActionContext.PSObject.TypeNames.Insert(0, 'BuildActionContext')

function DoSomethingWithBuildActionContext
{
[CmdletBinding()]
param
(
    [Parameter(Mandatory=$true)][BuildActionContext]$Context
)

Write-Host "Build action: $($Context.BuildAction)"
}

DoSomethingWithBuildActionContext -Context $buildActionContext

Working code, but can it be done differently:

$buildActionContext = New-Object -TypeName PSObject -Property @{
        # Given properties
        BuildAction = "Build"; 
    }
    $buildActionContext.PSObject.TypeNames.Insert(0, 'BuildActionContext')

function DoSomethingWithBuildActionContext
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory=$true)]$Context
    )

    if ($Context.PSObject.TypeNames[0] -ne 'BuildActionContext')
    {
        throw "Context parameter not of type 'BuildActionContext'"
    }

    Write-Host "Build action: $($Context.BuildAction)"
}

DoSomethingWithBuildActionContext -Context $buildActionContext
DoSomethingWithBuildActionContext -Context "Hello world"

Note: Second call gives the exception message.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T15:37:44+00:00Added an answer on May 16, 2026 at 3:37 pm

    I would expect that only real .NET types can be used to specify parameter type. According to Essential PowerShell: Name your custom object types the custom type names are mainly used for formatting.

    You can check the type names manually via ValidateScript attribute:

    function DoSomethingWithBuildActionContext { 
      param(
        [Parameter()]
        [ValidateScript({ $_.PSObject.TypeNames[0] -eq 'BuildActionContext' })]
        $context
      )
      Write-Host "Build action: $($Context.BuildAction)"
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to be able to define and use a custom type in
Its possible with TinyMCE to define custom URL converter logic as defined in this
I have a YAML formatted text file, and would like to define custom folding
I would like to know if I can define custom assembly attributes. Existing attributes
Is it possible to define custom names for methods like thing function foo() {
Is there a way to define custom templates with optional parameters? Something like %{
I am building an automation system using Powershell and I would like to define
Documentation states: Adds a user-defined custom member to an instance of a Windows PowerShell
When we define custom metadata for the components, it is my understanding that this
Is there a way to define custom codes (with messages/descriptions and whatnot) so that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.