How to pass credentials to the Send-MailMessage command for sending emails

I found this blog site: Adam Kahtava I also found this question: send-mail-via-gmail-with-powershell-v2s-send-mailmessage The problem is, neither of them addressed both your needs (Attachment with a password), so I did some combination of the two and came up with this: $EmailTo = “[email protected]” $EmailFrom = “[email protected]” $Subject = “Test” $Body = “Test Body” $SMTPServer = … Read more

How can I get the current PowerShell executing file?

I’ve tried to summarize the various answers here, updated for PowerShell 5: If you’re only using PowerShell 3 or higher, use $PSCommandPath If want compatibility with older versions, insert the shim: if ($PSCommandPath -eq $null) { function GetPSCommandPath() { return $MyInvocation.PSCommandPath; } $PSCommandPath = GetPSCommandPath; } This adds $PSCommandPath if it doesn’t already exist. The … Read more

How do I concatenate two text files in PowerShell?

Simply use the Get-Content and Set-Content cmdlets: Get-Content inputFile1.txt, inputFile2.txt | Set-Content joinedFile.txt You can concatenate more than two files with this style, too. If the source files are named similarly, you can use wildcards: Get-Content inputFile*.txt | Set-Content joinedFile.txt Note 1: PowerShell 5 and older versions allowed this to be done more concisely using … Read more

Prompt for user input in PowerShell

Read-Host is a simple option for getting string input from a user. $name = Read-Host ‘What is your username?’ To hide passwords you can use: $pass = Read-Host ‘What is your password?’ -AsSecureString To convert the password to plain text: [Runtime.InteropServices.Marshal]::PtrToStringAuto( [Runtime.InteropServices.Marshal]::SecureStringToBSTR($pass)) As for the type returned by $host.UI.Prompt(), if you run the code at … Read more