2

After upgrading to powershell 3.0 existing scripts stopped working with an error

ConvertTo-SecureString : The term 'ConvertTo-SecureString' is not recognized as the name of a cmdlet, function, script
file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct
and try again.
At line:1 char:1
+ ConvertTo-SecureString
+ ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (ConvertTo-SecureString:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

I found that ConvertTo-SecureString is supported on PS 3.0. Do I need to include it somehow?

Sly
  • 15,046
  • 12
  • 60
  • 89
  • Did *all* scripts stop working? Or only scripts with `ContertTo-SecureString` in them? – MDMoore313 Nov 13 '13 at 15:53
  • On my 3.0 the corresponding DLL is located here `C:\Windows\Microsoft.Net\assembly\GAC_MSIL\Microsoft.PowerShell.Security\v4.0_3.0.0.0__31bf3856ad364e35\Microsoft.PowerShell.Security.dll`. You might check to see if that dll is at that path. You should also have this directory: `C:\Windows\System32\WindowsPowerShell\v1.0\modules\Microsoft.PowerShell.Security` – Keith Hill Nov 13 '13 at 19:29
  • BTW which OS are you on? Do you have any server products installed like Exchange, SharePoint Server, SCCM, etc? – Keith Hill Nov 14 '13 at 05:29

2 Answers2

1

The following does NOT work.

C:\contoso>powershell -command {$secured='random text'|ConvertTo-SecureString -AsPlainText -Force;$secured;}

'ConvertTo-SecureString' is not recognized as an internal or external command,
operable program or batch file.

C:\contoso>

The following does work.

C:\contoso>copy con: tfile1.ps1
$secured='random text'|ConvertTo-SecureString -AsPlainText -Force;
$secured;
^Z
        1 file(s) copied.

C:\contoso>powershell -file tfile1.ps1
System.Security.SecureString

C:\contoso> 

This also works.

C:\contoso>powershell "& {$secured='random text'|ConvertTo-SecureString -AsPlainText -Force;$secured}"
System.Security.SecureString

C:\contoso>

I'll leave why it doesn't work as a -command to someone else as I'm only a powershell novice.

S.

  • Your answer is not related to the problem above, but I came here because I think I had the same problem as you (calling PS with `ConvertTo-SecureString` from cmd or PS). Your workaround is not based on the fact, that you use additional variables, but on the fact that you invoke the command a layer deeper. Just to clarify, this will also work: `powershell "& {ConvertTo-SecureString -AsPlainText 'random text' -Force}"` – stackprotector Apr 23 '20 at 11:28
-1
Import-Module 'Microsoft.PowerShell.Security'

fixes the issue. I don't know why this module is not loaded by default.

Sly
  • 15,046
  • 12
  • 60
  • 89