0

I have the following code example. I am just trying to print the value from a hashtable key.

function New-Article()
{
  param ($volume, $issue, $title)

  $article = @{}
  $article.volume = $volume
  $article.issue = $issue
  $article.title = $title

  return $article
}

$article = New-Article(1, 2, "Article Title")
Write-Host "Article title: $article.title" # Output = Article title: System.Collections.Hashtable.title
Write-Host "Article title: $($article.title)" # Output = Article title: 
Write-Host "Article volume: $($article.volume)" # Output = Article volume: 1 2 Article Title

$article = New-Article 1, 2, "Article Title"
Write-Host "Article title: $($article.title)" # Output = Article title: 

Edit added a line to test what is mentioned in the possible duplicate (relating to properties, not hashtables)

Edit Added more examples based on comments and answers

steveo225
  • 11,394
  • 16
  • 62
  • 114

1 Answers1

4

You have 2 problems. In addition to enclosing the hashtable property access expression in a $(), you are invoking the function incorrectly. In powershell, arguments are passed to a function separated by spaces, with no brackets:

function New-Article()
{
  param ($volume, $issue, $title)

  $article = @{}
  $article.volume = $volume
  $article.issue = $issue
  $article.title = $title

  return $article
}

$article = New-Article 1 2 "Article Title"
Write-Host "Article title: $($article.title)"
zdan
  • 28,667
  • 7
  • 60
  • 71