You have to execute the above script from a .ps1 file.
The $_ variable contains the object in the pipeline from the foreach statement.
Copy the code into a .ps1 file, then execute the .ps1 file by giving the path to the .ps1 file at the PS prompt.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference