1

Looking to use ConvertFrom-String on Linux as per the example from the help file:

**********************
PowerShell transcript start
Start time: 20230204142830
Username: mordor\nicholas
RunAs User: mordor\nicholas
Configuration Name: 
Machine: mordor (Unix 5.15.0.58)
Host Application: /snap/powershell/229/opt/powershell/pwsh.dll
Process ID: 1193147
PSVersion: 7.3.2
PSEdition: Core
GitCommitId: 7.3.2
OS: Linux 5.15.0-58-generic #64-Ubuntu SMP Thu Jan 5 11:43:13 UTC 2023
Platform: Unix
PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.10032.0, 6.0.0, 6.1.0, 6.2.0, 7.0.0, 7.1.0, 7.2.0, 7.3.2
PSRemotingProtocolVersion: 2.3
SerializationVersion: 1.1.0.1
WSManStackVersion: 3.0
**********************
Transcript started, output file is /home/nicholas/powershell/PowerShell_transcript.mordor.ciSjO_A6.20230204142830.txt
PS /home/nicholas/powershell> $template = @'
{Name*:Phoebe Cat}, {phone:425-123-6789}, {age:6}
{Name*:Lucky Shot}, {phone:(206) 987-4321}, {age:12}
'@
PS /home/nicholas/powershell> $testText = @'
Phoebe Cat, 425-123-6789, 6
Lucky Shot, (206) 987-4321, 12
Elephant Wise, 425-888-7766, 87
Wild Shrimp, (111)  222-3333, 1
'@
PS /home/nicholas/powershell> $PersonalData = $testText | ConvertFrom-String -TemplateContent $template

ConvertFrom-String: The term 'ConvertFrom-String' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

ConvertFrom-String: The term 'ConvertFrom-String' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
PS /home/nicholas/powershell> $PersonalData = $testText | ConvertFrom-StringData -TemplateContent $template
PS /home/nicholas/powershell> TerminatingError(ConvertFrom-StringData): "A parameter cannot be found that matches parameter name 'TemplateContent'."

ConvertFrom-StringData: A parameter cannot be found that matches parameter name 'TemplateContent'.

ConvertFrom-StringData: A parameter cannot be found that matches parameter name 'TemplateContent'.
PS /home/nicholas/powershell> help ConvertFrom-String
PS /home/nicholas/powershell> help ConvertFrom-StringData
PS /home/nicholas/powershell> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.3.2
PSEdition                      Core
GitCommitId                    7.3.2
OS                             Linux 5.15.0-58-generic #64-Ubuntu SMP Thu Jan 5 11:43:13 UTC 2023
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

PS /home/nicholas/powershell> lsb_release -a
No LSB modules are available.
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 22.04 LTS
Release:    22.04
Codename:   jammy

what throws me off here is that the help file loads fine, which would indicate that the component is installed. Perhaps that's an incorrect understanding.

  • 1
    Check the environmental variable PSModulePath. See : https://github.com/PowerShell/PowerShellGet/issues/218 – jdweng Feb 04 '23 at 22:41
  • 1
    @jdweng, `ConvertFrom-String` is a Windows PowerShell-only module, which is therefore by definition unavailable on _Linux_. (On _Windows_, it is _automatically_ available via the Windows PowerShell Compatibility feature, but there are good reasons to avoid it). – mklement0 Feb 04 '23 at 23:09
  • 1
    Note: `ConvertFrom-String` is not to be confused with the [`ConvertFrom-StringData`](https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/convertfrom-stringdata) cmdlet, which _is_ available in PowerShell (Core) as well, on all supported platforms. – mklement0 Feb 04 '23 at 23:11

1 Answers1

1

ConvertFrom-String is available only in Windows PowerShell, the legacy, Windows-only edition of PowerShell - it was never ported to PowerShell (Core), the modern, cross-platform edition.

  • On Windows only, the cmdlet is technically still available, via the Windows PowerShell compatibility feature (which comes with its own limitations); however, for the reasons stated below, it's best to avoid this cmdlet altogether.

  • Note: ConvertFrom-String is not to be confused with the ConvertFrom-StringData cmdlet, which is available in PowerShell (Core) as well, on all supported platforms.

However, even in Windows PowerShell / on Windows there are good reasons to avoid use of this cmdlet:

  • It provides separator-based parsing as well as heuristics-based parsing based on templates containing example values.
  • The separator-based parsing applies automatic type conversions you cannot control, and the poorly documented template language results in behavior that is inherently hard to predict.
mklement0
  • 382,024
  • 64
  • 607
  • 775
  • So `regex` would be a reasonable approach on Linux? – Nicholas Saunders Feb 05 '23 at 02:16
  • 1
    @NicholasSaunders, either that, or a `Convert-FromStringData` approach, possibly with some pre-processing, as suggested in [this answer](https://stackoverflow.com/a/75303797/45375) to one of your previous questions. Honestly, `ConvertFrom-String` is a dead end, and I suggest neither using it nor accepting answers that use it. – mklement0 Feb 05 '23 at 03:24