![]() ![]() It performs Trim() operation in order to remove space characters from the beginning and end of the variable/property.Therefore you can use any characters in the value field. It splits only on the first occurrence of the character "=".So it resolves the error saying that nameĬould not be created because it is an empty string. It ignores commented lines and new lines in the file.txt before.This is an improvement to the Shay Levy's answer. ![]() If you have at least PowerShell v3 or higher you can simplify the process (assuming you want a custom psobject) $values = (Get-Content $Path -Raw | ConvertFrom-StringData) Your input file can also have varying leading and trailing space around the equals sign which will give the exact same result.ĭepending on your use case you can take that one step farther and create a custom object from that hashtable New-Object -TypeName pscustomobject -Property $values No need to know where the properties are in respect to the file. Where the $values.midasServer would have the value serv8194. $values = Get-Content $Path | Out-String | ConvertFrom-StringData So if a text file contained just the data in your example you could do this to create a hashtable $Path = "C:\temp\test.txt" Because each key/value pair must be on a separate line, here-strings are often used as the input format. a list of key value pairs denoted with a equals sign then you should have a look at ConvertFrom-StringData whichĬonverts a string that contains one or more key and value pairs into a hash table. If that is exactly how your file appears i.e. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |