{"id":810194,"date":"2026-05-20T14:07:08","date_gmt":"2026-05-20T14:07:08","guid":{"rendered":"https:\/\/www.ninjaone.com\/?post_type=script_hub&#038;p=810194"},"modified":"2026-05-20T14:07:09","modified_gmt":"2026-05-20T14:07:09","slug":"remover-um-computador-de-um-dominio-usando-o-powershell","status":"publish","type":"script_hub","link":"https:\/\/www.ninjaone.com\/pt-br\/script-hub\/remover-um-computador-de-um-dominio-usando-o-powershell\/","title":{"rendered":"Como remover um computador de um dom\u00ednio usando o PowerShell"},"content":{"rendered":"<p>No sofisticado ecossistema de gerenciamento de TI, \u00e9 fundamental garantir que os dispositivos sejam corretamente conectados ou removidos de um dom\u00ednio de rede. Para os profissionais de TI, essa tarefa geralmente \u00e9 repetitiva, mas a import\u00e2ncia de uma execu\u00e7\u00e3o perfeita n\u00e3o pode ser subestimada. Hoje, vamos nos aprofundar em um script do PowerShell criado para <strong>remover um computador de um dom\u00ednio<\/strong> sem esfor\u00e7o.<\/p>\n<h2>Hist\u00f3rico<\/h2>\n<p>O script fornecido atende \u00e0 necessidade espec\u00edfica de automatizar as opera\u00e7\u00f5es de sa\u00edda do dom\u00ednio. Os dom\u00ednios servem para conglomerar contas de usu\u00e1rios, computadores e pol\u00edticas de grupo em um \u00fanico local centralizado. H\u00e1 v\u00e1rios motivos pelos quais um profissional de TI pode querer remover um computador de um dom\u00ednio, seja por seguran\u00e7a, altera\u00e7\u00f5es na infraestrutura ou desativa\u00e7\u00e3o de hardware. A execu\u00e7\u00e3o manual dessa tarefa n\u00e3o s\u00f3 consome tempo como tamb\u00e9m est\u00e1 sujeita a erros humanos. Portanto, um script robusto do PowerShell como o descrito acima \u00e9 inestim\u00e1vel para profissionais de TI e <a href=\"https:\/\/www.ninjaone.com\/what-is-an-msp\/\">provedores de servi\u00e7os gerenciados (MSPs)<\/a>.<\/p>\n<h2>O roteiro<\/h2>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"powershell\">#Requires -Version 2.0\r\n\r\n&lt;#\r\n.SYNOPSIS\r\n    Removes the computer from the domain.\r\n.DESCRIPTION\r\n    Removes the computer from the domain.\r\n.EXAMPLE\r\n     -UserName \"MyDomainUser\" -Password \"Somepass1\" -LocalUserName \"Administrator\" -LocalPassword \"Somepass1\"\r\n    Removes the computer from the domain and restarts the computer.\r\n.EXAMPLE\r\n     -UserName \"MyDomainUser\" -Password \"Somepass1\" -LocalUserName \"Administrator\" -LocalPassword \"Somepass1\" -NoRestart\r\n    Removes the computer from the domain and does not restart the computer.\r\n.EXAMPLE\r\n    PS C:&gt; Leave-Domain.ps1 -UserName \"MyDomainUser\" -Password \"Somepass1\" -LocalUserName \"Administrator\" -LocalPassword \"Somepass1\" -NoRestart\r\n    Removes the computer from the domain and does not restart the computer.\r\n.OUTPUTS\r\n    String[]\r\n.NOTES\r\n    Minimum OS Architecture Supported: Windows 7, Windows Server 2012\r\n    Release Notes:\r\n    Initial Release\r\nBy using this script, you indicate your acceptance of the following legal terms as well as our Terms of Use at https:\/\/www.ninjaone.com\/terms-of-use.\r\n    Ownership Rights: NinjaOne owns and will continue to own all right, title, and interest in and to the script (including the copyright). NinjaOne is giving you a limited license to use the script in accordance with these legal terms. \r\n    Use Limitation: You may only use the script for your legitimate personal or internal business purposes, and you may not share the script with another party. \r\n    Republication Prohibition: Under no circumstances are you permitted to re-publish the script in any script library or website belonging to or under the control of any other software provider. \r\n    Warranty Disclaimer: The script is provided \u201cas is\u201d and \u201cas available\u201d, without warranty of any kind. NinjaOne makes no promise or guarantee that the script will be free from defects or that it will meet your specific needs or expectations. \r\n    Assumption of Risk: Your use of the script is at your own risk. You acknowledge that there are certain inherent risks in using the script, and you understand and assume each of those risks. \r\n    Waiver and Release: You will not hold NinjaOne responsible for any adverse or unintended consequences resulting from your use of the script, and you waive any legal or equitable rights or remedies you may have against NinjaOne relating to your use of the script. \r\n    EULA: If you are a NinjaOne customer, your use of the script is subject to the End User License Agreement applicable to you (EULA).\r\n.COMPONENT\r\n    ManageUsers\r\n#&gt;\r\n\r\n[CmdletBinding()]\r\nparam (\r\n    # Use a Domain UserName to remove this computer to a domain, this requires the Password parameter to be used as well\r\n    [Parameter(Mandatory = $true)]\r\n    [String]\r\n    $UserName,\r\n    # Use a Domain Password to remove a computer from a domain\r\n    [Parameter(Mandatory = $true)]\r\n    $Password,\r\n    # Use a local admin's UserName to remove this computer from a domain, this requires the Password parameter to be used as well\r\n    [Parameter(Mandatory = $true)]\r\n    [String]\r\n    $LocalUserName,\r\n    # Use a local admin's Password to remove this computer from a domain\r\n    [Parameter(Mandatory = $true)]\r\n    $LocalPassword,\r\n    # Do not restart computer after leaving to a domain\r\n    [Switch]\r\n    $NoRestart\r\n)\r\n    \r\nbegin {\r\n    Write-Output \"Starting Leave Domain\"\r\n    \r\n    # Converts username and password into a credential object\r\n    $LeaveCred = [PSCredential]::new($UserName, $(ConvertTo-SecureString -String $Password -AsPlainText -Force))\r\n    \r\n    # Converts username and password into a credential object\r\n    $LocalCred = [PSCredential]::new($LocalUserName, $(ConvertTo-SecureString -String $LocalPassword -AsPlainText -Force))\r\n}\r\n    \r\nprocess {\r\n    Write-Output \"Removing computer($env:COMPUTERNAME) from domain\"\r\n    $script:LeaveResult = $false\r\n    try {\r\n        $LeaveResult = if ($NoRestart) {\r\n            (Remove-Computer -UnjoinDomainCredential $LeaveCred -PassThru -Force -LocalCredential $LocalCred -Confirm:$false).HasSucceeded\r\n            # Do not restart after leaving\r\n        }\r\n        else {\r\n            # Restart after leaving\r\n            (Remove-Computer -UnjoinDomainCredential $LeaveCred -PassThru -Force -Restart -LocalCredential $LocalCred -Confirm:$false).HasSucceeded\r\n        }    \r\n    }\r\n    catch {\r\n        Write-Error \"Failed to Leave Domain\"\r\n    }\r\n    if ($LeaveResult) {\r\n        if ($NoRestart) {\r\n            Write-Output \"Removed computer($env:COMPUTERNAME) from domain and not restarting computer\"\r\n        }\r\n        else {\r\n            Write-Output \"Removed computer($env:COMPUTERNAME) from domain and restarting computer\"\r\n        }\r\n    }\r\n    else {\r\n        Write-Output \"Failed to remove computer($env:COMPUTERNAME) from domain\"\r\n        # Clean up credentials so that they don't leak outside this script\r\n        $LeaveCred = $null\r\n        $LocalCred = $null\r\n        exit 1\r\n    }\r\n}\r\n    \r\nend {\r\n    # Clean up credentials so that they don't leak outside this script\r\n    $LeaveCred = $null\r\n    $LocalCred = $null\r\n    Write-Output \"Completed Leave Domain\"\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n\n<div class=\"in-context-cta\"><p style=\"text-align: center;\">Acesse mais de 300 scripts no NinjaOne Dojo<\/p>\n<p style=\"text-align: center;\"><a href=\"https:\/\/www.ninjaone.com\/freetrialform\/\">Obter acesso<\/a><\/p>\n<\/div>\n<h2>Detalhamento<\/h2>\n<p>O script segue uma abordagem modular, abrangendo tr\u00eas se\u00e7\u00f5es principais: <strong>in\u00edcio<\/strong>, <strong>processo<\/strong> e <strong>fim<\/strong>.<\/p>\n<ul>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"1\" data-list-defn-props=\"{&quot;335552541&quot;:1,&quot;335559684&quot;:-2,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769226&quot;:&quot;Symbol&quot;,&quot;469769242&quot;:[8226],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;\uf0b7&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" data-aria-posinset=\"1\" data-aria-level=\"1\">Na se\u00e7\u00e3o <strong>begin<\/strong>, ele inicializa o script, aceita par\u00e2metros como dom\u00ednio e nomes de usu\u00e1rio\/senhas locais e constr\u00f3i objetos de credenciais a partir desses par\u00e2metros.<\/li>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"1\" data-list-defn-props=\"{&quot;335552541&quot;:1,&quot;335559684&quot;:-2,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769226&quot;:&quot;Symbol&quot;,&quot;469769242&quot;:[8226],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;\uf0b7&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" data-aria-posinset=\"2\" data-aria-level=\"1\">A se\u00e7\u00e3o <strong>de processo<\/strong> \u00e9 onde ocorre a opera\u00e7\u00e3o real. Ele usa o cmdlet <strong>Remove-Computer<\/strong> do PowerShell, uma ferramenta avan\u00e7ada projetada para remover o computador de seu dom\u00ednio atual. A flexibilidade do script permite a op\u00e7\u00e3o de reiniciar o computador ap\u00f3s a remo\u00e7\u00e3o ou deix\u00e1-lo em execu\u00e7\u00e3o, dependendo da prefer\u00eancia do usu\u00e1rio.<\/li>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"1\" data-list-defn-props=\"{&quot;335552541&quot;:1,&quot;335559684&quot;:-2,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769226&quot;:&quot;Symbol&quot;,&quot;469769242&quot;:[8226],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;\uf0b7&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" data-aria-posinset=\"2\" data-aria-level=\"1\">A se\u00e7\u00e3o <strong>final<\/strong> trata da limpeza, garantindo que as credenciais usadas sejam apagadas para evitar poss\u00edveis problemas de seguran\u00e7a.<\/li>\n<\/ul>\n<h2>Casos de uso em potencial<\/h2>\n<p>Imagine um profissional de TI, Alex, gerenciando a infraestrutura de rede de uma organiza\u00e7\u00e3o de m\u00e9dio porte. Eles acabaram de desativar v\u00e1rias esta\u00e7\u00f5es de trabalho antigas, substituindo-as por novas. Em vez de desassociar manualmente cada computador do dom\u00ednio, Alex usa esse script, economizando horas de trabalho e garantindo que nenhuma credencial residual seja deixada nas m\u00e1quinas desativadas.<\/p>\n<h2>Compara\u00e7\u00f5es<\/h2>\n<p>Embora o m\u00e9todo baseado em GUI, feito por meio de &#8220;Propriedades do sistema&#8221;, ofere\u00e7a uma abordagem mais visual, o <a href=\"https:\/\/www.ninjaone.com\/blog\/how-to-automate-tasks-with-powershell\/\">script do PowerShell se destaca pela automa\u00e7\u00e3o<\/a>, escalabilidade e precis\u00e3o. Para remover um \u00fanico computador, a GUI pode ser suficiente, mas para opera\u00e7\u00f5es em massa, o PowerShell \u00e9 incompar\u00e1vel.<\/p>\n<h2>Implica\u00e7\u00f5es<\/h2>\n<p>O uso de scripts como esse para gerenciar as opera\u00e7\u00f5es do dom\u00ednio n\u00e3o \u00e9 apenas uma quest\u00e3o de conveni\u00eancia, mas de seguran\u00e7a. Garantir que as credenciais n\u00e3o vazem, como o script faz corretamente, \u00e9 de suma import\u00e2ncia. Uma remo\u00e7\u00e3o de dom\u00ednio mal gerenciada pode deixar brechas para os invasores cibern\u00e9ticos.<\/p>\n<h2>Recomenda\u00e7\u00f5es<\/h2>\n<ul>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"1\" data-list-defn-props=\"{&quot;335552541&quot;:1,&quot;335559684&quot;:-2,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769226&quot;:&quot;Symbol&quot;,&quot;469769242&quot;:[8226],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;\uf0b7&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" data-aria-posinset=\"1\" data-aria-level=\"1\">Sempre teste o script em um ambiente controlado antes de implement\u00e1-lo em um cen\u00e1rio real.<\/li>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"1\" data-list-defn-props=\"{&quot;335552541&quot;:1,&quot;335559684&quot;:-2,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769226&quot;:&quot;Symbol&quot;,&quot;469769242&quot;:[8226],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;\uf0b7&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" data-aria-posinset=\"2\" data-aria-level=\"1\">Certifique-se de que os mecanismos de backup estejam em vigor, especialmente ao fazer altera\u00e7\u00f5es em massa.<\/li>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"1\" data-list-defn-props=\"{&quot;335552541&quot;:1,&quot;335559684&quot;:-2,&quot;335559685&quot;:720,&quot;335559991&quot;:360,&quot;469769226&quot;:&quot;Symbol&quot;,&quot;469769242&quot;:[8226],&quot;469777803&quot;:&quot;left&quot;,&quot;469777804&quot;:&quot;\uf0b7&quot;,&quot;469777815&quot;:&quot;hybridMultilevel&quot;}\" data-aria-posinset=\"2\" data-aria-level=\"1\">Atualize regularmente o script para acomodar altera\u00e7\u00f5es no sistema operacional ou nos cmdlets do PowerShell.<\/li>\n<\/ul>\n<h2>Considera\u00e7\u00f5es finais<\/h2>\n<p>Embora os scripts do PowerShell, como o discutido, ofere\u00e7am imensa utilidade, o aproveitamento de plataformas como o NinjaOne pode otimizar ainda mais as opera\u00e7\u00f5es de TI. O NinjaOne fornece uma <a href=\"https:\/\/www.ninjaone.com\/pt-br\/endpoint-management\/\">plataforma unificada de monitoramento e gerenciamento de TI<\/a>, integrando perfeitamente <a href=\"https:\/\/www.ninjaone.com\/blog\/category\/scripts\/\">scripts<\/a>, automa\u00e7\u00e3o e uma infinidade de tarefas de TI sob o mesmo teto. Para os profissionais que buscam otimizar suas tarefas de gerenciamento de TI, ferramentas como esse script, quando integradas a plataformas como o NinjaOne, oferecem um horizonte promissor.<\/p>\n","protected":false},"author":35,"featured_media":144235,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_acf_changed":false,"_relevanssi_hide_post":"","_relevanssi_hide_content":"","_relevanssi_pin_for_all":"","_relevanssi_pin_keywords":"","_relevanssi_unpin_keywords":"","_relevanssi_related_keywords":"","_relevanssi_related_include_ids":"","_relevanssi_related_exclude_ids":"","_relevanssi_related_no_append":"","_relevanssi_related_not_related":"","_relevanssi_related_posts":"","_relevanssi_noindex_reason":"","_lmt_disableupdate":"no","_lmt_disable":""},"operating_system":[4212],"use_cases":[12460],"class_list":["post-810194","script_hub","type-script_hub","status-publish","has-post-thumbnail","hentry","script_hub_category-windows"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.ninjaone.com\/pt-br\/wp-json\/wp\/v2\/script_hub\/810194","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ninjaone.com\/pt-br\/wp-json\/wp\/v2\/script_hub"}],"about":[{"href":"https:\/\/www.ninjaone.com\/pt-br\/wp-json\/wp\/v2\/types\/script_hub"}],"author":[{"embeddable":true,"href":"https:\/\/www.ninjaone.com\/pt-br\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ninjaone.com\/pt-br\/wp-json\/wp\/v2\/comments?post=810194"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.ninjaone.com\/pt-br\/wp-json\/wp\/v2\/media\/144235"}],"wp:attachment":[{"href":"https:\/\/www.ninjaone.com\/pt-br\/wp-json\/wp\/v2\/media?parent=810194"}],"wp:term":[{"taxonomy":"script_hub_category","embeddable":true,"href":"https:\/\/www.ninjaone.com\/pt-br\/wp-json\/wp\/v2\/operating_system?post=810194"},{"taxonomy":"use_cases","embeddable":true,"href":"https:\/\/www.ninjaone.com\/pt-br\/wp-json\/wp\/v2\/use_cases?post=810194"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}