You can put any error messages generated by a cmdlet into a variable with the ErrorVariable common parameter. Then, you can save the error in a file by piping the variable into Out-File.
PS C:\Windows\system32> Get-Service kkkkkk -ErrorVariable e Get-Service : Cannot find any service with service name 'kkkkkk'. At line:1 char:1 + Get-Service kkkkkk -ErrorVariable e + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (kkkkkk:String) [Get-Service], ServiceCommandException + FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.GetServiceCommand PS C:\Windows\system32> $e Get-Service : Cannot find any service with service name 'kkkkkk'. At line:1 char:1 + Get-Service kkkkkk -ErrorVariable e + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (kkkkkk:String) [Get-Service], ServiceCommandException + FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.GetServiceCommand PS C:\Windows\system32> $e | Out-File c:\error.txt
You can append other errors to the error varible like this: Get-Service bbbbbbbbb -ErrorVariable +e
You can also have a look at the help file: about_CommonParameters
I hope it helped!