Powershell dump in C with variable name

Good Morning,

I found a script to read out memberships of a User from active directory as per below.

       [Parameter(Mandatory = $True)]
Import-Module ActiveDirectory
If ($UserName) {
       $UserName = $UserName.ToUpper().Trim()
       $Res = (Get-ADPrincipalGroupMembership $UserName | Measure-Object).Count
       If ($Res -GT 0) {
              Write-Output "`n"
              Write-Output "The User $UserName Is A Member Of The Following Groups:"
              Write-Output "==========================================================="
              Get-ADPrincipalGroupMembership $UserName | Select-Object -Property Name, GroupScope, GroupCategory | Sort-Object -Property Name | FT -A

This has been tested and worked fine.

Please guide me on how to Save the outcome in C$ +with the usename variable.txt and then step two copy the txt file from C:\ to remote share (xcopy).

All this has to be within the same powershelll command.

Thanks in advance.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Sorry, but that script is, well, suboptimal.
Try it with this; you can now pass a string array with file names to save the results to. The file name(s) may contain the placeholder "{0}", which will be replaced with the UserName passed.
If the file extension is '.csv', it will save as csv, otherwise as plain text.
.\Whatever.ps1 -UserName jdoe -Path "C:\Groups_{0}.txt", "\\Server\Share\Folder\Groups_{0}.txt"

Open in new window

If you always want to save to the same locations, you can just set the Path argument in line 5 to the default values you want:
	[String[]]$Path = "C:\Groups_{0}.txt", "\\Server\Share\Folder\Groups_{0}.txt"

Open in new window

Import-Module ActiveDirectory
$groups = Get-ADPrincipalGroupMembership -Identity $UserName |
	Select-Object -Property @{n='UserName'; e={$UserName}}, Name, GroupScope, GroupCategory |
	Sort-Object -Property Name
If ($Path) {
	ForEach ($filePath In $Path) {
		$filePath = $filePath -f $UserName
		If ([IO.Path]::GetExtension($filePath) -eq '.csv') {
			$groups | Export-Csv -NoTypeInformation -Path $filePath
		} Else {
			$groups | Format-Table -AutoSize | Out-String -Width 1024 | Set-Content -Path $filePath
} Else {

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Question answered.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.