A number of PowerShell related tips and tricks.

John Savill

September 12, 2017

3 Min Read
Digital globe

Three times a week (Monday/Wednesday/Friday), John Savill tackles your most pressing IT questions.

Read through the FAQ archives, or send him your questions via email.

Q. How can I join a machine to a domain using PowerShell automatically?
Q. How can I remove a sequence of characters from the end of a string in PowerShell?
Q. I'm calling a function in my PowerShell but if I Write-Output in the function it does not output the screen but rather is returned as part of the return value, why?

Q. How can I join a machine to a domain using PowerShell automatically?
Dept - PowerShell

A. The code below joins a machine to a domain unattended.

$user = "savilltechlocaladmin"
$password = 'ssss'
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ($user, $securePassword)
Add-Computer -DomainName savilltech.net -OUPath "OU=Servers,DC=savilltech,DC=net" -Credential $cred -Restart

Q. How can I remove a sequence of characters from the end of a string in PowerShell?
Dept - PowerShell

A. While strings have their own functions to trim characters they may not behave as expected. For example TrimEnd removes a sequence of characters from the end of a string however the emphasis is characters, i.e. it will remove any instance of those characters from the end of the string and not the string you specify. For example:

PS C:Usersjohn> $testvalue = "folkejoke"

PS C:Usersjohn> $testend = $testvalue.TrimEnd("joke")

PS C:Usersjohn> $testend
fol

I should have ended with folke but instead fol. The reason is it keeps removing each character in the string "joke" until it comes to a letter not in the string. Folke ends with ke so those characters are removed as well! Instead I like to use substring starting from the beginning of the string and then trimming when it finds the first occurrence of what I want to remove. For example:

PS C:Usersjohn> $testvalue = "folkejoke"

PS C:Usersjohn> $testtrimmed = $testvalue.Substring(0,$testvalue.IndexOf("joke"))

PS C:Usersjohn> $testtrimmed
folke

Note this assume the word is only in the source string once. Another approach would be to just remove the number of characters from the string equal to the length of the word to be trimmed. For example:

PS C:Usersjohn> $testvalue.Substring(0,($testvalue.Length-"joke".Length))
folke

Q. I'm calling a function in my PowerShell but if I Write-Output in the function it does not output the screen but rather is returned as part of the return value, why?
Dept - PowerShell

A. Write-Output does not actually write to the screen. Write-Output sends information to the PowerShell output which if not captured anywhere ultimately gets sent to the host. If however you use this within a function then the Write-Output content is returned from the function. For example:

function test
{
$returnVal = "howdy"
Write-Output "Completed return value set"
return $returnVal
}

$howdy = test

On executing nothing is written to screen. If you inspect $howdy you will see it contains all the text.

PS C:Usersjohn> $howdy
Completed return value set
howdy

One option would be to use Write-Host in the function however that would only write to the screen making it impossible to capture the functions other output. Another would be to have the function write to a file for its output if that was the ultimate target.

A better solution is within the function create a custom object and return that. One attribute could contain return text, the other the return value. For example:

function test
{
$returnVal = "howdy"
$returntext = "Completed return value set"
$returntext += " some more text"
$returnobj = @{
Text = $returntext
Value = $returnVal
}

return (New-Object PSObject -Property $returnobj)
}

$howdy = test
Now if you inspect $howdy it has two attributes.
PS C:Usersjohn> $howdy.Text
Completed return value set some more text

PS C:Usersjohn> $howdy.Value
howdy

Mission accomplished!

 

 

 

 

 

About the Author

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like