{"id":17461,"date":"2010-08-11T00:01:00","date_gmt":"2010-08-11T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/08\/11\/masking-passwords-in-windows-powershell\/"},"modified":"2010-08-11T00:01:00","modified_gmt":"2010-08-11T00:01:00","slug":"masking-passwords-in-windows-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/masking-passwords-in-windows-powershell\/","title":{"rendered":"Masking Passwords in Windows PowerShell"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p><strong>Summary<\/strong>: With Windows PowerShell cmdlets, it is easy to mask passwords used when scripting remote applications.<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Question\" border=\"0\" title=\"Hey, Scripting Guy! Question\" \/> Hey, Scripting Guy! I need to be able to mask a password when running a <a href=\"http:\/\/technet.microsoft.com\/en-us\/scriptcenter\/powershell.aspx\">Windows PowerShell<\/a> script. I am attempting to convert my VBScript scripts to Windows PowerShell, but I have a problem. In VBScript I would use a COM object called <strong>scriptpw.password<\/strong>. I first learned about it from the <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2005\/02\/04\/how-can-i-mask-passwords-using-an-inputbox.aspx\">How Can I Mask Passwords Using an InputBox?<\/a> Hey, Scripting Guy! post. Unfortunately, I get an error when attempting to use this object in Windows PowerShell. I don&rsquo;t get it because I thought that Windows PowerShell could create COM objects. What do I need to do to make this work?<\/p>\n<p>&#8212; AJ<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Answer\" border=\"0\" title=\"Hey, Scripting Guy! Answer\" \/> Hello AJ, <\/p>\n<p>Microsoft Scripting Guy Ed Wilson here. I was in Ft. Lauderdale, <a href=\"http:\/\/en.wikipedia.org\/wiki\/Florida\">Florida<\/a>, teaching a VBScript class a few years ago when this happened. There was a knock on the hotel room door. I opened it, and there stood my significant other. Teresa had decided to fly down and join me for the weekend. Because she went to all the trouble to fly down and visit me, I felt compelled to see what she wanted to do for the weekend. For her, the choice was easy&mdash;go to a butterfly farm. Luckily, I had my camera with me. Butterfly farms offer myriad opportunities for taking great pictures, such as this one I snapped.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7802.hsg081110011_2AABAB06.jpg\"><img decoding=\"async\" height=\"454\" width=\"604\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1145.hsg081110011_thumb_644E44F0.jpg\" alt=\"Photo Ed took of a butterfly\" border=\"0\" title=\"Photo Ed took of a butterfly\" style=\"border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px\" \/><\/a> <\/p>\n<p>Even though it might seem difficult to take great pictures of butterflies, it can be surprisingly easy. AJ, the same is true for masking passwords in Windows PowerShell: It is very easy when you know the trick. For example, if I need to mask the administrator password while checking on the BIOS version of a remote computer, I use the <strong>&ndash;credential<\/strong> parameter:<\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #0000ff\">Get-WmiObject<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Class<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">win32_bios<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-ComputerName<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">hyperv<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Credential<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">nwtraders\\administrator<\/span> <\/p>\n<p>When the <strong>Get-WmiObject<\/strong> command runs, the dialog box appears that is shown in the following image. Note that the username is automatically filled out from the domain and username supplied when typing the command. When the Windows PowerShell Credential Request dialog box appears, the password characters are masked as you type them. <\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7802.hsg081110021_23ABF881.jpg\"><img decoding=\"async\" height=\"399\" width=\"500\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1122.hsg081110021_thumb_7C057C56.jpg\" alt=\"Image of dialog box that appears when Get-WmiObject command is run\" border=\"0\" title=\"Image of dialog box that appears when Get-WmiObject command is run\" style=\"border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px\" \/><\/a> <\/p>\n<p>If you do not supply a username for the <strong>&ndash;credential<\/strong> parameter for the <strong>Get-WmiObject<\/strong> cmdlet, the error shown in the following image appears.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3386.hsg081110031_66A7C6EE.jpg\"><img decoding=\"async\" height=\"199\" width=\"604\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8407.hsg081110031_thumb_663B93F9.jpg\" alt=\"Image of error produced when a  user name is not supplied for -credential parameter\" border=\"0\" title=\"Image of error produced when a  user name is not supplied for -credential parameter\" style=\"border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px\" \/><\/a> <\/p>\n<p>The error actually makes sense because when working interactively, you would normally supply the username to use for the command. <\/p>\n<p>There are 56 cmdlets that use the <strong>&ndash;credential<\/strong> parameter. They are shown here. <strong>GCM<\/strong> is an alias for the <strong>Get-Command<\/strong> cmdlet. The <strong>?<\/strong> is an alias for the <strong>Where-Object<\/strong> cmdlet:<\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #0000ff\">PS<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">C:\\&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">gcm<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-type<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">cmdlet<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">|<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">?<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$_<\/span><span style=\"color: #000000\">.parameters.keys<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">-match<\/span><span style=\"color: #808080\"> &#8216;credential&#8217;<\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">|<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">Format-Wide<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #000000\">name<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-AutoSize<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #000000\">Add-Computer<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">Add-Content<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">Clear-Content<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #0000ff\">Clear-Item<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">Clear-ItemProperty<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Connect-WSMan<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #0000ff\">Copy-Item<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">Copy-ItemProperty<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Enter-PSSession<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #0000ff\">Get-Content<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">Get-Credential<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Get-HotFix<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #0000ff\">Get-Item<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">Get-ItemProperty<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Get-WinEvent<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #0000ff\">Get-WmiObject<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Get-WSManInstance<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Invoke-Command<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #0000ff\">Invoke-Item<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Invoke-WmiMethod<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Invoke-WSManAction<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #0000ff\">Join-Path<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">Move-Item<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">Move-ItemProperty<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #0000ff\">New-Item<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">New-ItemProperty<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">New-PSDrive<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #000000\">New-PSSession<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">New-PSSessionOption<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">New-Service<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #000000\">New-WebServiceProxy<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">New-WSManInstance<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">New-WSManSessionOption<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #000000\">Register-WmiEvent<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Remove-Computer<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">Remove-Item<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #0000ff\">Remove-ItemProperty<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Remove-WmiObject<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Remove-WSManInstance<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #0000ff\">Rename-Item<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">Rename-ItemProperty<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">Resolve-Path<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #000000\">Restart-Computer<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Send-MailMessage<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">Set-Content<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #0000ff\">Set-Item<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">Set-ItemProperty<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Set-WmiInstance<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #000000\">Set-WSManInstance<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">Split-Path<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Start-Job<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #000000\">Start-Process<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Stop-Computer<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Test-Connection<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #0000ff\">Test-Path<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Test-WSMan<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #0000ff\">PS<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">C:\\&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">gcm<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-type<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">cmdlet<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">|<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">?<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$_<\/span><span style=\"color: #000000\">.parameters.keys<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">-match<\/span><span style=\"color: #808080\"> &#8216;credential&#8217;<\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">|<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">measure<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #000000\">Count<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">:<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">56<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #000000\">Average<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">:<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #000000\">Sum<\/span><span style=\"color: #808080\">&amp;nb\nsp;<\/span><span style=\"color: #000000\">:<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #000000\">Maximum<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">:<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #000000\">Minimum<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">:<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #000000\">Property<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">:<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #0000ff\">PS<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">C:\\&gt;<\/span> <\/p>\n<p>Unfortunately, most of the 56 cmdlets that accept the credential parameter may not work the way you envision. For example, the <strong>Get-Item<\/strong> cmdlet accepts credential, but it is not implemented for any of the default providers except for the <strong>wsMan<\/strong> drive. This is shown here: <\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #0000ff\">PS<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">C:\\&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">Get-PSProvider<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #000000\">Name<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Capabilities<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Drives<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #000000\">&#8212;-<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">&#8212;&#8212;&#8212;&#8212;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">&#8212;&#8212;<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #000000\">WSMan<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Credentials<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{WSMan}<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #000000\">Alias<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">ShouldProcess<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{Alias}<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #000000\">Environment<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">ShouldProcess<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{Env}<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #000000\">FileSystem<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Filter,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">ShouldProcess<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{C,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">E,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">D}<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #000000\">Function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">ShouldProcess<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{Function}<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #000000\">Registry<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">ShouldProcess,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Transactions<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{HKLM,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">HKCU}<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #000000\">Variable<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">ShouldProcess<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{Variable}<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #000000\">Certificate<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">ShouldProcess<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{cert}<\/span><span style=\"color: #808080\">      <br \/><\/span><span style=\"color: #0000ff\">PS<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">C:\\&gt;<\/span> <\/p>\n<p>This means that you cannot supply credentials for the <strong>Get-Item<\/strong> cmdlet when connecting to registry or to a file system drive&mdash;two of the more common scenarios. <\/p>\n<p>If you would like the user to type full credentials for your script (both a user name and a password), you can use the <strong>Get-Credential<\/strong> cmdlet. This is shown in the ShutdownComputer.ps1 script.<\/p>\n<p style=\"padding-left: 30px\"><strong>ShutdownComputer.ps1<\/strong><\/p>\n<p style=\"padding-left: 30px\"><span style=\"color: #2b91af\">$credential<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">Get-Credential<\/span><span style=\"color: #808080\">      <\/p>\n<p><\/span><span style=\"color: #000000\">Stop-Computer<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-ComputerName<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">win7-c1<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Credential<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$credential<\/span> <\/p>\n<p>&nbsp;<\/p>\n<p>When the script runs, the dialog box appears that is shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8004.hsg081110041_13BCB3BD.jpg\"><img decoding=\"async\" height=\"399\" width=\"500\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4452.hsg081110041_thumb_65632E0F.jpg\" alt=\"Image of dialog box that appears when script is run\" border=\"0\" title=\"Image of dialog box that appears when script is run\" style=\"border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px\" \/><\/a> <\/p>\n<p>If you only want to obtain the password, you can use the <strong>Read-Host<\/strong> cmdlet with the <strong>&ndash;asSecureString<\/strong> parameter. By default, when using the <strong>Read-Host<\/strong> cmdlet, it does not mask data supplied via the command line. But when the <strong>&ndash;asSecureString<\/strong> parameter is used, it masks the password. This is shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6740.hsg081110051_72C94115.jpg\"><img decoding=\"async\" height=\"219\" width=\"604\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2627.hsg081110051_thumb_446FBB68.jpg\" alt=\"Image of -asSecureString masking password\" border=\"0\" title=\"Image of -asSecureString masking password\" style=\"border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px\" \/><\/a> <\/p>\n<p>Because both of these methods, <strong>Get-Credential<\/strong> and <strong>Read-Host<\/strong>, return an instance of the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.security.securestring.aspx\">System.Security.SecureString<\/a> .NET Framework object, it is important to test your script to ensure that the target application will accept a <strong>SecureString<\/strong> for the password. This is especially true for old COM interfaces. Most .NET Framework interfaces will accept the <strong>SecureString<\/strong>. This is important from a security concern because a <strong>SecureString<\/strong> is encrypted. <\/p>\n<p>AJ, that is all there is to using Windows PowerShell to mask passwords when running scripts. Running Windows PowerShell Scripts Week will continue tomorrow when we will talk about pausing a script to wait for user interaction. <\/p>\n<p>We invite you to follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to us at <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/social.technet.microsoft.com\/Forums\/en\/ITCG\/threads\/\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Summary: With Windows PowerShell cmdlets, it is easy to mask passwords used when scripting remote applications. &nbsp; Hey, Scripting Guy! I need to be able to mask a password when running a Windows PowerShell script. I am attempting to convert my VBScript scripts to Windows PowerShell, but I have a problem. In VBScript I [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[51,171,2,3,4,45],"class_list":["post-17461","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-masking-passwords","tag-running","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>&nbsp; Summary: With Windows PowerShell cmdlets, it is easy to mask passwords used when scripting remote applications. &nbsp; Hey, Scripting Guy! I need to be able to mask a password when running a Windows PowerShell script. I am attempting to convert my VBScript scripts to Windows PowerShell, but I have a problem. In VBScript I [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17461","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=17461"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17461\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=17461"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=17461"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=17461"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}