# What's the most secure way to ask for / pass in credentials?

**URL:** <https://forums.powershell.org/t/whats-the-most-secure-way-to-ask-for-pass-in-credentials/22664>\
**Category:** PowerShell Help\
**Created:** [July 26, 2023, 12:01am UTC](https://forums.powershell.org/t/whats-the-most-secure-way-to-ask-for-pass-in-credentials/22664 "2023-07-26T00:01:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![shanebagel](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@shanebagel](https://forums.powershell.org/u/shanebagel)\
**Post date:** [July 26, 2023, 12:01am UTC](https://forums.powershell.org/t/whats-the-most-secure-way-to-ask-for-pass-in-credentials/22664/1 "2023-07-26T00:01:00Z")

</div>

I have a script that I want to ask the user who runs it to type in credentials - I don’t want to use pscredential - because it prompts for a username - which I DON’T need:

```
[CmdletBinding()]
Param (   
    [Parameter(ValueFromPipeline = $True, Mandatory = $False)]
    [ValidateNotNull()]
    [SecureString]$Credential,

)

if (-not $Credential) {
    Write-Host "Please enter a password for the new user:"
    $Credential = Read-Host -AsSecureString
}

```

Is this bad practice? Is there a better way to ask for credentials - the only ways I know are with pscredential which relies on Get-Credential. Is this not secure?

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [July 26, 2023, 12:11am UTC](https://forums.powershell.org/t/whats-the-most-secure-way-to-ask-for-pass-in-credentials/22664/2 "2023-07-26T00:11:52Z")

</div>

> [@shanebagel](#):
>
> I don’t want to use pscredential - because it prompts for a username

This is incorrect. You can specify the username

```auto
Get-Credential username

Get-Credential username@domain.com

Get-Credential domain\username

```

Now if you don’t want the user to see and/or be able to change the username, then Read-Host -AsSecureString would be the way to go. However, I’d still use a pscredential

```auto
$securepassword = Read-Host "Please enter a password for the new user" -AsSecureString
$credential = New-Object PSCredential $username, $securepassword

```

---

<div class="post-metadata">

**Author:** ![shanebagel](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@shanebagel](https://forums.powershell.org/u/shanebagel)\
**Post date:** [July 26, 2023, 12:22am UTC](https://forums.powershell.org/t/whats-the-most-secure-way-to-ask-for-pass-in-credentials/22664/3 "2023-07-26T00:22:50Z")

</div>

Perfect thank youuuuu

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:14pm UTC](https://forums.powershell.org/t/whats-the-most-secure-way-to-ask-for-pass-in-credentials/22664/4 "2024-05-16T20:14:44Z")

</div>


