# How to convert a password in a securestring to use it in an other script.

**URL:** <https://forums.powershell.org/t/how-to-convert-a-password-in-a-securestring-to-use-it-in-an-other-script/4991>\
**Category:** PowerShell Help\
**Created:** [October 8, 2015, 1:15am UTC](https://forums.powershell.org/t/how-to-convert-a-password-in-a-securestring-to-use-it-in-an-other-script/4991 "2015-10-08T01:15:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![luca-wirths](https://avatars.discourse-cdn.com/v4/letter/l/77aa72/32.png) [@luca-wirths](https://forums.powershell.org/u/luca-wirths)\
**Post date:** [October 8, 2015, 1:15am UTC](https://forums.powershell.org/t/how-to-convert-a-password-in-a-securestring-to-use-it-in-an-other-script/4991/1 "2015-10-08T01:15:04Z")

</div>

Hi,  
I am trying to make a script which runs automatic. It have to open an remote session. But there I have troble with the credentials.  
I saved them to pw.txt. But if I want to use them it wont work.  
How can I fix that?

Thanks for help.

```
#save the password
read-host -assecurestring | convertfrom-securestring | out-file C:\temp\pw.txt

#use the password
$pass = cat C:\temp\pw.txt | ConvertTo-SecureString
$mycred = new-object -TypeName System.Management.Automation.PSCredential -ArgumentList "Domain\Userl", $pass

New-PSSession -ComputerName dc -Credential $mycred
```

---

<div class="post-metadata">

**Author:** ![tim-lane](https://avatars.discourse-cdn.com/v4/letter/t/439d5e/32.png) [@tim-lane](https://forums.powershell.org/u/tim-lane)\
**Post date:** [October 8, 2015, 2:09am UTC](https://forums.powershell.org/t/how-to-convert-a-password-in-a-securestring-to-use-it-in-an-other-script/4991/2 "2015-10-08T02:09:25Z")

</div>

This works ok for me. What error are you getting?

---

<div class="post-metadata">

**Author:** ![mark-hammonds](https://avatars.discourse-cdn.com/v4/letter/m/90db22/32.png) [@mark-hammonds](https://forums.powershell.org/u/mark-hammonds)\
**Post date:** [October 8, 2015, 2:21am UTC](https://forums.powershell.org/t/how-to-convert-a-password-in-a-securestring-to-use-it-in-an-other-script/4991/3 "2015-10-08T02:21:01Z")

</div>

I work across four domains so here is what I use. it will get the FQDN and pull the server name off leaving you with just the domain then build the file for credentials for each domain encountered… I know its a bit long winded but im a newbie.

```
# Begin load Credentials
$serverdomain = ("$($Server.Split(".")[-2])." + "$($Server.Split(".")[-1])")
    if(!(test-path "C:\temp\encrypted.csv")){
        if(!(test-path "c:\temp")) {MD "C:\temp"}
             $secure = Get-Credential -Message "Please provide a password for $serverdomain\" -UserName "$serverdomain\"
            $username = $secure.UserName
            $password = convertfrom-securestring -secureString $secure.Password -key (1..16)
            $hashedcredentials.add($username, $password)
            $outputtable = $hashedcredentials.GetEnumerator() | foreach{ New-Object PSObject -Property([ordered]@{Username = $_.Name;Password = $_.Value})}
            $outputtable | Export-Csv c:\temp\encrypted.csv -NoTypeInformation
    }

$credentials = Import-Csv "C:\temp\encrypted.csv"  
    $adminname = $credentials | ?{$_.username -like "*$serverdomain*"} | %{$_.username}
    $password = $credentials | ?{$_.username -like "*$serverdomain*"} | %{$_.password} | convertto-securestring -key (1..16)  

 $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $adminname,$password
```

---

<div class="post-metadata">

**Author:** ![luca-wirths](https://avatars.discourse-cdn.com/v4/letter/l/77aa72/32.png) [@luca-wirths](https://forums.powershell.org/u/luca-wirths)\
**Post date:** [October 8, 2015, 2:45am UTC](https://forums.powershell.org/t/how-to-convert-a-password-in-a-securestring-to-use-it-in-an-other-script/4991/4 "2015-10-08T02:45:02Z")

</div>

Thanks a lot.  
Looks great! I try it tomorrow.

---

<div class="post-metadata">

**Author:** ![luca-wirths](https://avatars.discourse-cdn.com/v4/letter/l/77aa72/32.png) [@luca-wirths](https://forums.powershell.org/u/luca-wirths)\
**Post date:** [October 8, 2015, 5:01pm UTC](https://forums.powershell.org/t/how-to-convert-a-password-in-a-securestring-to-use-it-in-an-other-script/4991/5 "2015-10-08T17:01:22Z")

</div>

Im also an very newbie…  
The script doesnt work.  
Ive only set the $Server variable. But it looks like i have to set also the $hashedcredentials variable. How does it look?

---

<div class="post-metadata">

**Author:** ![mark-hammonds](https://avatars.discourse-cdn.com/v4/letter/m/90db22/32.png) [@mark-hammonds](https://forums.powershell.org/u/mark-hammonds)\
**Post date:** [October 9, 2015, 1:38am UTC](https://forums.powershell.org/t/how-to-convert-a-password-in-a-securestring-to-use-it-in-an-other-script/4991/6 "2015-10-09T01:38:30Z")

</div>

your right I forgot to include this at the top of the script.  
$hashedcredentials = @{}

This is what you want…

```
$hashedcredentials = @{}

Foreach($Server in $Servers){
$serverdomain = ("$($Server.Split(".")[-2])." + "$($Server.Split(".")[-1])") 

    ######################################Domain Credentials#################################################
if(test-path "C:\temp\encrypted.csv"){
$credentials = Import-Csv "C:\temp\encrypted.csv"
$adminname = $credentials | ?{$_.username -like "*$serverdomain*"} | %{$_.username}
$password = $credentials | ?{$_.username -like "*$serverdomain*"} | %{$_.password} | convertto-securestring -key (1..16)
} 
    if($adminname -eq $null){
            $secure = Get-Credential -Message "Please provide a password for $serverdomain\" -UserName "$serverdomain\"
            $username = $secure.UserName
            $password = convertfrom-securestring -secureString $secure.Password -key (1..16)
            $hashedcredentials.add($username, $password)
            $outputtable = $hashedcredentials.GetEnumerator() | foreach{ New-Object PSObject -Property([ordered]@{Username = $_.Name;Password = $_.Value})}
            $outputtable | Export-Csv c:\temp\encrypted.csv -Append -NoTypeInformation 
            $credentials = Import-Csv "C:\temp\encrypted.csv" 
            $adminname = $credentials | ?{$_.username -like "*$serverdomain*"} | %{$_.username}
            $password = $credentials | ?{$_.username -like "*$serverdomain*"} | %{$_.password} | convertto-securestring -key (1..16) 
    }
    else{    
        $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $adminname,$password
    }       
    #########################################################################################################
}
```

---

<div class="post-metadata">

**Author:** ![luca-wirths](https://avatars.discourse-cdn.com/v4/letter/l/77aa72/32.png) [@luca-wirths](https://forums.powershell.org/u/luca-wirths)\
**Post date:** [October 9, 2015, 3:16am UTC](https://forums.powershell.org/t/how-to-convert-a-password-in-a-securestring-to-use-it-in-an-other-script/4991/7 "2015-10-09T03:16:14Z")

</div>

You’ve made my day! Thanks alot!

---

<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:44pm UTC](https://forums.powershell.org/t/how-to-convert-a-password-in-a-securestring-to-use-it-in-an-other-script/4991/8 "2024-05-16T20:44:41Z")

</div>

Closed on May 16, 2024, 8:44pm UTC


