You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在New-ADOrganizationalUnit cmdlet中使用错误变量与错误操作?

Troubleshooting "New-ADOrganizationalUnit is not recognized" Error

Hey there, let's break down what's happening here first:

That error message isn't about your error handling parameters (-ea SilentlyContinue or -ErrorVariable myerror) failing—it's a pre-execution problem: PowerShell can't even find the New-ADOrganizationalUnit cmdlet in the first place. Those error-handling flags only work once the cmdlet exists and starts running; they don't help with "cmdlet not found" errors.

Why this happens

New-ADOrganizationalUnit is part of the ActiveDirectory PowerShell module, which isn't installed or loaded by default on all Windows systems. Here's how to fix it:


Step 1: Install the ActiveDirectory Module

For Windows Server:

Run this command in an elevated PowerShell window to install the RSAT AD PowerShell feature:

Install-WindowsFeature RSAT-AD-PowerShell

For Windows 10/11 (Client OS):

You can install it via Settings or command line:

  • Settings path: Go to Settings > Apps > Optional features > Add a feature, search for "RSAT: Active Directory Domain Services and Lightweight Directory Tools", expand it, check "Active Directory module for Windows PowerShell", and install.
  • Command line (elevated):
    Get-WindowsCapability -Name RSAT.ActiveDirectory* -Online | Add-WindowsCapability -Online
    

Step 2: Load the Module (if not auto-loaded)

Even after installation, the module might not load automatically. Run this in your PowerShell session:

Import-Module ActiveDirectory

Step 3: Verify the Cmdlet Exists

To confirm everything is set up, run:

Get-Command New-ADOrganizationalUnit

If it returns a result, you're good to go.


Once the module is ready...

Your original command will work as expected, and -ea SilentlyContinue will handle execution-time errors (like if the OU already exists). For example:

New-ADOrganizationalUnit -Name $INNCODE -Path "OU=???,OU=???,DC=??,DC=???,DC=???,DC=???" -ea SilentlyContinue -ErrorVariable myerror

Now, if the OU already exists, PowerShell will silently skip the error instead of throwing a message, and the error details will be stored in $myerror.

内容的提问来源于stack exchange,提问作者Nikita Pandey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:39:41