{"id":8252,"date":"2021-03-26T15:50:07","date_gmt":"2021-03-26T19:50:07","guid":{"rendered":"https:\/\/jdhitsolutions.com\/blog\/?p=8252"},"modified":"2021-03-26T15:50:12","modified_gmt":"2021-03-26T19:50:12","slug":"answering-the-powershell-export-challenge","status":"publish","type":"post","link":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8252\/answering-the-powershell-export-challenge\/","title":{"rendered":"Answering the PowerShell Export Challenge"},"content":{"rendered":"\n<div class=\"wp-block-image is-style-default\"><figure class=\"alignleft size-large\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/03\/pexels-karolina-grabowska-4498136.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"160\" height=\"240\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/03\/pexels-karolina-grabowska-4498136.jpg\" alt=\"\" class=\"wp-image-8257\"\/><\/a><\/figure><\/div>\n\n\n\n<p>Last month, the Iron Scripter Chairman put out <a href=\"https:\/\/ironscripter.us\/a-powershell-session-challenge-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">a rather large and complex challenge.<\/a> The basic premise of the challenge was to export a PowerShell session  to a file, and then import it in later PowerShell session. In essence, the save the working state of your PowerShell session. This would include items such as defined aliases, variables, functions, PSDrives, PSSessions, and more. Short of running in a virtual machine or maybe a container, where you could save the entire state, this is a tall order. But not insurmountable<\/p>\n\n\n\n<p>In some instances, PowerShell has specific commands, like Export-Alias and Import-Alias. And as a last resort, there is always Export-CliXml. This is a preferred format because it will capture rich object data. I approached the problem by working on an export and import process for different elements. Here's a look at a few of them.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Master HashTable<\/h2>\n\n\n\n<p>My export process begins with a master hashtable. The hashtable starts with some metadata.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">$master\u00a0=\u00a0[ordered]@{\n\u00a0\u00a0\u00a0\u00a0Computername\u00a0=\u00a0[System.Environment]::MachineName\n\u00a0\u00a0\u00a0\u00a0Username\u00a0\u00a0\u00a0\u00a0\u00a0=\u00a0[System.Environment]::UserName\n\u00a0\u00a0\u00a0\u00a0Date\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0=\u00a0Get-Date\n\u00a0\u00a0\u00a0\u00a0PSVersion\u00a0\u00a0\u00a0\u00a0=\u00a0\"{0}.{1}\"\u00a0-f\u00a0$PSVersionTable.PSVersion.Major,\u00a0$PSVersionTable.PSVersion.Minor\n\u00a0\u00a0\u00a0\u00a0Host\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0=\u00a0$host.Name\n\u00a0\u00a0\u00a0\u00a0Version\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0=\u00a0$host.version\n}<\/code><\/pre>\n\n\n\n<p>I'll be adding to this hashtable and eventually exporting using Export-CliXML.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">PrivateData<\/h2>\n\n\n\n<p>One item you might change and want to persist across sessions is $host.PrivateData. For example, I change the error color to Green to make it easier to read. To save this information, I created hashtable using the PSObject so that I could enumerate names and values.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">$pdHash\u00a0=\u00a0@{}\n$host.PrivateData.psobject.properties\u00a0|\u00a0ForEach-Object\u00a0-Process\u00a0{\u00a0$pdhash.Add($_.name,\u00a0$_.value)\u00a0}\n$master.Add(\"PrivateData\",\u00a0$pdHash)<\/code><\/pre>\n\n\n\n<p>To import, once I imported the cliXML file, $data is the saved PrivateData. Because it is a hashtable, it is easy to unroll and then set $host.privatedata settings.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">$data.GetEnumerator()\u00a0|\u00a0ForEach-Object\u00a0{\n\u00a0\u00a0\u00a0\u00a0Write-Verbose\u00a0\"Setting\u00a0private\u00a0data\u00a0value\u00a0for\u00a0$($_.name)\"\n\u00a0\u00a0\u00a0\u00a0if\u00a0($pscmdlet.ShouldProcess($_.Name,\u00a0\"Set\u00a0value\u00a0to\u00a0$($_.value.value)\"))\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$host.privatedata.$($_.name)\u00a0=\u00a0$_.value\n\u00a0\u00a0\u00a0\u00a0}\n}\u00a0<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Variables<\/h2>\n\n\n\n<p>Variables aren't difficult to export. But there was no reason to export default variables. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">$exclude\u00a0=\u00a0\"PWD\",\u00a0\"PSCulture\",\u00a0\"Profile\",\u00a0\"`$\",\u00a0\"?\",\u00a0\"^\",\u00a0\"false\",\u00a0\"true\",\u00a0\"host\",\n\"nestedpromptlevel\",\u00a0\"home\",\u00a0\"MyInvocation\",\u00a0\"Pid\",\u00a0\"PSEdition\",\u00a0\"PSHome\",\u00a0\"PSVersionTable\",\u00a0\"pwd\",\n\"ShellID\",\u00a0\"StackTrace\",\u00a0\"NestedpromptLevel\",\u00a0\"PSUiCulture\",\u00a0\"ConsoleFileName\",\u00a0\"Error\",\u00a0\"ExecutionContext\",\n\"OutputEncoding\",\u00a0\"PSBoundParameters\",\u00a0\"PSCmdlet\",\u00a0\"Passthru\",\u00a0\"PSScriptRoot\"\n\n$myVariables\u00a0=\u00a0Get-Variable\u00a0-Scope\u00a0global\u00a0|\u00a0Where-Object\u00a0{\u00a0$exclude\u00a0-notcontains\u00a0$_.name\u00a0}<\/code><\/pre>\n\n\n\n<p>To restore the variables, I use Set-Variable and splat parameters based on the imported objects.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">foreach\u00a0($var\u00a0in\u00a0$data)\u00a0{\n\u00a0\u00a0\u00a0\u00a0Write-Verbose\u00a0\"Adding\u00a0$($var.name)\"\n\u00a0\u00a0\u00a0\u00a0$vHash\u00a0=\u00a0@{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Name\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0=\u00a0$var.name\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Description\u00a0=\u00a0$var.Description\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Visibility\u00a0\u00a0=\u00a0$var.Visibility\u00a0-as\u00a0[System.Management.Automation.SessionStateEntryVisibility]\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Option\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0=\u00a0$var.options\u00a0-as\u00a0[System.Management.Automation.ScopedItemOptions]\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Value\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0=\u00a0$var.value\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Force\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0=\u00a0$True\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Scope\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0=\u00a0\"Global\"\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ErrorAction\u00a0=\u00a0\"Stop\"\n\u00a0\u00a0\u00a0\u00a0}\n\n\u00a0\u00a0\u00a0\u00a0Try\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Set-Variable\u00a0@vHash\n\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0Catch\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Write-Warning\u00a0\"Skipping\u00a0variable\u00a0$($vhash.name).\u00a0$($_.Exception.message)\"\n\u00a0\u00a0\u00a0\u00a0}\n\n}\u00a0<em>#foreach<\/em><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">PSSessions<\/h2>\n\n\n\n<p>Exporting PSSessions was slightly complicated. I knew that I'd be using New-PSSession to recreate the sessions. So I needed to export all of the information I would need for that step. I'm only exporting open sessions. I also needed to take PSSessionOptions into account.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">$sessions\u00a0=\u00a0Get-PSSession\u00a0|\u00a0Where-Object\u00a0{\u00a0$_.state\u00a0-eq\u00a0\"Opened\"\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$all\u00a0=\u00a0foreach\u00a0($session\u00a0in\u00a0$sessions)\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$ci\u00a0=\u00a0$session.runspace.connectioninfo\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$obj\u00a0=\u00a0[ordered]@{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0PSTypeName\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0=\u00a0\"ExportedPSSession\"\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Computername\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0=\u00a0$session.Computername\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ComputerType\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0=\u00a0$session.Computertype\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ConfigurationName\u00a0\u00a0\u00a0\u00a0\u00a0=\u00a0$session.ConfigurationName\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Credential\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0=\u00a0$ci.credential\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Username\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0=\u00a0$ci.Username\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0CertificateThumbprint\u00a0=\u00a0$ci.CertificateThumbprint\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Port\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0=\u00a0$ci.Port\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Transport\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0=\u00a0$session.transport\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<em>#add\u00a0connection\u00a0info<\/em>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$ciProperties\u00a0=\u00a0@(\"MaximumReceivedDataSizePerCommand\",\u00a0\"MaximumReceivedObjectSize\",\u00a0\"NoMachineProfile\",\u00a0\"ProxyAccessType\",\u00a0\"ProxyAuthentication\",\u00a0\"ProxyCredential\",\u00a0\"SkipCACheck\",\u00a0\"SkipCNCheck\",\u00a0\"SkipRevocationCheck\",\u00a0\"NoEncryption\",\u00a0\"UseUTF16\",\u00a0\"OutputBufferingMode\",\u00a0\"IncludePortInSPN\",\u00a0\"MaxConnectionRetryCount\",\u00a0\"Culture\",\u00a0\"UICulture\",\u00a0\"OpenTimeout\",\u00a0\"CancelTimeout\",\u00a0\"OperationTimeout\",\u00a0\"IdleTimeout\")\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$ciHash\u00a0=\u00a0@{}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0foreach\u00a0($property\u00a0in\u00a0$ciProperties)\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<em>#\"\u00a0$property\u00a0=\u00a0$($ci.$property)\"<\/em>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if\u00a0($null\u00a0-ne\u00a0$ci.$property)\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$ciHash.Add($property,\u00a0$ci.$property)\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0else\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Write-Host\u00a0\"Skipping\u00a0$property\"\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$obj.Add(\"ConnectionInfo\",\u00a0$ciHash)\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0New-Object\u00a0-TypeName\u00a0PSObject\u00a0-Property\u00a0$obj\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$master.Add(\"PSSessions\",\u00a0$all)<\/code><\/pre>\n\n\n\n<p>This gives me a rich object for each PSSession. Here's how I rebuild it in a new sesssion.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">Foreach\u00a0($session\u00a0in\u00a0$data)\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$params\u00a0=\u00a0@{ErrorAction\u00a0=\u00a0\"Stop\"\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Write-Verbose\u00a0\"Processing\u00a0session\u00a0for\u00a0$($session.computername)\"\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if\u00a0($session.CertificateThumbprint)\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$params.Add(\"CertificateThumbprint\".$session.CertificateThumbprint)\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if\u00a0($session.configurationName\u00a0-AND\u00a0($session.transport\u00a0-ne\u00a0'SSH'))\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$params.Add(\"ConfigurationName\",\u00a0$session.ConfigurationName)\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if\u00a0($session.credential)\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$params.add(\"Credential\",\u00a0$session.credential)\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if\u00a0($session.port\u00a0-AND\u00a0($session.port\u00a0-notmatch\u00a0\"5985|80\"))\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Write-Verbose\u00a0\"Using\u00a0custom\u00a0port\u00a0$($session.port)\"\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$params.Add(\"Port\",\u00a0$session.port)\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if\u00a0($session.transport\u00a0-eq\u00a0'SSH')\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$params.Add(\"Hostname\",\u00a0$session.computername)\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$params.Add(\"SSHTransport\",\u00a0$True)\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if\u00a0($session.Username)\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$params.Add(\"UserName\",\u00a0$session.userName)\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0elseif\u00a0($session.Computertype.value\u00a0-eq\u00a0'VirtualMachine')\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Write-Verbose\u00a0\"Connecting\u00a0to\u00a0virtual\u00a0machine\u00a0$($session.ComputerName)\"\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$params.Add(\"VMName\",\u00a0$session.computername)\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0elseif\u00a0($session.computertype.value\u00a0-eq\u00a0\"RemoteMachine\")\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Write-Verbose\u00a0\"Connecting\u00a0to\u00a0remote\u00a0computer\u00a0$($session.ComputerName)\"\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$params.Add(\"Computername\",\u00a0$session.computername)\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$ci\u00a0=\u00a0$session.ConnectionInfo\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$opt\u00a0=\u00a0New-PSSessionOption\u00a0@ci\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$params.Add(\"SessionOption\",\u00a0$opt)\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0else\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Write-Warning\u00a0\"Computertype\u00a0$($session.Computertype.value)\u00a0not\u00a0handled\u00a0at\u00a0this\u00a0time.\"\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$skip\u00a0=\u00a0$True\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if\u00a0(-Not\u00a0$skip)\u00a0{\n\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Try\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0[void](New-PSSession\u00a0@params)\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Catch\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Write-Warning\u00a0\"Failed\u00a0to\u00a0recreate\u00a0PSSession\u00a0for\u00a0$($session.Computername).\u00a0$($_.Exception.Message)\"\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$params\u00a0|\u00a0Out-String\u00a0|\u00a0Write-Verbose\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}<\/code><\/pre>\n\n\n\n<p>This should handle almost any type of PSSession.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">CIMSessions<\/h2>\n\n\n\n<p>These type of remoting sessions, were a bit trickier because any credential information used isn't part of the object. So if exported, I need to prompt the user for credentials.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">$cim\u00a0=\u00a0Get-CimSession\u00a0|\u00a0Where-Object\u00a0{\u00a0$_.TestConnection()\u00a0}\u00a0|\nSelect-Object\u00a0Computername,\u00a0Protocol,\n@{Name\u00a0=\u00a0\"Credential\";\u00a0Expression\u00a0=\u00a0{\u00a0Get-Credential\u00a0-Message\u00a0\"Enter\u00a0a\u00a0credential\u00a0for\u00a0the\u00a0$($_.computername.toUpper())\u00a0CIMSession\u00a0if\u00a0needed\u00a0or\u00a0click\u00a0cancel\"\u00a0;\u00a0}\u00a0}\n\n$master.Add(\"CimSessions\",\u00a0$cim)<\/code><\/pre>\n\n\n\n<p>The only CIMSessionOption I'm accounting for is Protocol. Otherwise, recreating the CIMSession isn't that difficult.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"powershell\" class=\"language-powershell\">$data\u00a0|\u00a0ForEach-Object\u00a0{\n\u00a0\u00a0\u00a0\u00a0$params\u00a0=\u00a0@{Computername\u00a0=\u00a0$_.computername\u00a0}\n\u00a0\u00a0\u00a0\u00a0if\u00a0($_.protocol\u00a0-eq\u00a0\"DCOM\")\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$params.add(\"SessionOption\",\u00a0(New-CimSessionOption\u00a0-Protocol\u00a0DCOM))\n\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0if\u00a0($_.credential.username)\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$params.add(\"Credential\",\u00a0$_.credential)\n\u00a0\u00a0\u00a0\u00a0}\n\u00a0\u00a0\u00a0\u00a0[void](New-CimSession\u00a0@params)\n}\u00a0<em>#foreach<\/em><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">PSSessionExport<\/h2>\n\n\n\n<p>I put all of the related commands into a module called PSSessionExport. I wrote a single command, Export-PSWorkingSession, that creates the master XML file using Export-CliXML.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/03\/export.png\"><img loading=\"lazy\" decoding=\"async\" width=\"816\" height=\"274\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/03\/export.png\" alt=\"\" class=\"wp-image-8254\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/03\/export.png 816w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/03\/export-300x101.png 300w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/03\/export-768x258.png 768w\" sizes=\"auto, (max-width: 816px) 100vw, 816px\" \/><\/a><\/figure>\n\n\n\n<p>I made exporting PSSessions and CimSessions optional. The command will work in both Windows PowerShell and PowerShell 7.x. <\/p>\n\n\n\n<p> Because I included metadata, I wrote a simple command to get information about the export.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/03\/get.png\"><img loading=\"lazy\" decoding=\"async\" width=\"637\" height=\"242\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/03\/get.png\" alt=\"\" class=\"wp-image-8255\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/03\/get.png 637w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/03\/get-300x114.png 300w\" sizes=\"auto, (max-width: 637px) 100vw, 637px\" \/><\/a><\/figure>\n\n\n\n<p>You can only import into a PowerShell session running the same major version. But the process is pretty quick. Start a new session, import the module, and import the saved PowerShell working session.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><a href=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/03\/import.png\"><img loading=\"lazy\" decoding=\"async\" width=\"820\" height=\"494\" src=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/03\/import.png\" alt=\"\" class=\"wp-image-8256\" srcset=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/03\/import.png 820w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/03\/import-300x180.png 300w, https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/03\/import-768x463.png 768w\" sizes=\"auto, (max-width: 820px) 100vw, 820px\" \/><\/a><\/figure>\n\n\n\n<p>I included support for -WhatIf and plenty of Verbose output so you can see what is happening.<\/p>\n\n\n\n<p>Want to try? Since this is a proof-of-concept, I don't plan in publishing this to the PowerShell Gallery. But you can get the code from the<a href=\"https:\/\/github.com\/jdhitsolutions\/PSSessionExport\" target=\"_blank\" rel=\"noreferrer noopener\"> Github repository<\/a>. I'm not really planning on maintaining this, at least not on a regular basis. However, I have enabled Discussions on the repository.<\/p>\n\n\n\n<p>Enjoy!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Last month, the Iron Scripter Chairman put out a rather large and complex challenge. The basic premise of the challenge was to export a PowerShell session to a file, and then import it in later PowerShell session. In essence, the save the working state of your PowerShell session. This would include items such as defined&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"New on the blog: Answering the #PowerShell Console Export Challenge","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[4,8],"tags":[294,618,221,534],"class_list":["post-8252","post","type-post","status-publish","format-standard","hentry","category-powershell","category-scripting","tag-export-clixml","tag-iron-scripter","tag-module","tag-powershell"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Answering the PowerShell Export Challenge &#8226; The Lonely Administrator<\/title>\n<meta name=\"description\" content=\"My solution for an Iron Scripter PowerShell scripting challenge to export and import a PowerShell working console session.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jdhitsolutions.com\/blog\/powershell\/8252\/answering-the-powershell-export-challenge\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Answering the PowerShell Export Challenge &#8226; The Lonely Administrator\" \/>\n<meta property=\"og:description\" content=\"My solution for an Iron Scripter PowerShell scripting challenge to export and import a PowerShell working console session.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jdhitsolutions.com\/blog\/powershell\/8252\/answering-the-powershell-export-challenge\/\" \/>\n<meta property=\"og:site_name\" content=\"The Lonely Administrator\" \/>\n<meta property=\"article:published_time\" content=\"2021-03-26T19:50:07+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-03-26T19:50:12+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/03\/pexels-karolina-grabowska-4498136.jpg\" \/>\n<meta name=\"author\" content=\"Jeffery Hicks\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@JeffHicks\" \/>\n<meta name=\"twitter:site\" content=\"@JeffHicks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jeffery Hicks\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8252\\\/answering-the-powershell-export-challenge\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8252\\\/answering-the-powershell-export-challenge\\\/\"},\"author\":{\"name\":\"Jeffery Hicks\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"headline\":\"Answering the PowerShell Export Challenge\",\"datePublished\":\"2021-03-26T19:50:07+00:00\",\"dateModified\":\"2021-03-26T19:50:12+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8252\\\/answering-the-powershell-export-challenge\\\/\"},\"wordCount\":571,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"image\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8252\\\/answering-the-powershell-export-challenge\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/03\\\/pexels-karolina-grabowska-4498136.jpg\",\"keywords\":[\"Export-Clixml\",\"Iron Scripter\",\"module\",\"PowerShell\"],\"articleSection\":[\"PowerShell\",\"Scripting\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8252\\\/answering-the-powershell-export-challenge\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8252\\\/answering-the-powershell-export-challenge\\\/\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8252\\\/answering-the-powershell-export-challenge\\\/\",\"name\":\"Answering the PowerShell Export Challenge &#8226; The Lonely Administrator\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8252\\\/answering-the-powershell-export-challenge\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8252\\\/answering-the-powershell-export-challenge\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/03\\\/pexels-karolina-grabowska-4498136.jpg\",\"datePublished\":\"2021-03-26T19:50:07+00:00\",\"dateModified\":\"2021-03-26T19:50:12+00:00\",\"description\":\"My solution for an Iron Scripter PowerShell scripting challenge to export and import a PowerShell working console session.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8252\\\/answering-the-powershell-export-challenge\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8252\\\/answering-the-powershell-export-challenge\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8252\\\/answering-the-powershell-export-challenge\\\/#primaryimage\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/03\\\/pexels-karolina-grabowska-4498136.jpg\",\"contentUrl\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/wp-content\\\/uploads\\\/2021\\\/03\\\/pexels-karolina-grabowska-4498136.jpg\",\"width\":160,\"height\":240},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/powershell\\\/8252\\\/answering-the-powershell-export-challenge\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"PowerShell\",\"item\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/category\\\/powershell\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Answering the PowerShell Export Challenge\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/\",\"name\":\"The Lonely Administrator\",\"description\":\"Practical Advice for the Automating IT Pro\",\"publisher\":{\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/jdhitsolutions.com\\\/blog\\\/#\\\/schema\\\/person\\\/d0258030b41f07fd745f4078bdf5b6c9\",\"name\":\"Jeffery Hicks\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\",\"caption\":\"Jeffery Hicks\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Answering the PowerShell Export Challenge &#8226; The Lonely Administrator","description":"My solution for an Iron Scripter PowerShell scripting challenge to export and import a PowerShell working console session.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8252\/answering-the-powershell-export-challenge\/","og_locale":"en_US","og_type":"article","og_title":"Answering the PowerShell Export Challenge &#8226; The Lonely Administrator","og_description":"My solution for an Iron Scripter PowerShell scripting challenge to export and import a PowerShell working console session.","og_url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8252\/answering-the-powershell-export-challenge\/","og_site_name":"The Lonely Administrator","article_published_time":"2021-03-26T19:50:07+00:00","article_modified_time":"2021-03-26T19:50:12+00:00","og_image":[{"url":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/03\/pexels-karolina-grabowska-4498136.jpg","type":"","width":"","height":""}],"author":"Jeffery Hicks","twitter_card":"summary_large_image","twitter_creator":"@JeffHicks","twitter_site":"@JeffHicks","twitter_misc":{"Written by":"Jeffery Hicks","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8252\/answering-the-powershell-export-challenge\/#article","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8252\/answering-the-powershell-export-challenge\/"},"author":{"name":"Jeffery Hicks","@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"headline":"Answering the PowerShell Export Challenge","datePublished":"2021-03-26T19:50:07+00:00","dateModified":"2021-03-26T19:50:12+00:00","mainEntityOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8252\/answering-the-powershell-export-challenge\/"},"wordCount":571,"commentCount":0,"publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"image":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8252\/answering-the-powershell-export-challenge\/#primaryimage"},"thumbnailUrl":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/03\/pexels-karolina-grabowska-4498136.jpg","keywords":["Export-Clixml","Iron Scripter","module","PowerShell"],"articleSection":["PowerShell","Scripting"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/jdhitsolutions.com\/blog\/powershell\/8252\/answering-the-powershell-export-challenge\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8252\/answering-the-powershell-export-challenge\/","url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8252\/answering-the-powershell-export-challenge\/","name":"Answering the PowerShell Export Challenge &#8226; The Lonely Administrator","isPartOf":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8252\/answering-the-powershell-export-challenge\/#primaryimage"},"image":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8252\/answering-the-powershell-export-challenge\/#primaryimage"},"thumbnailUrl":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/03\/pexels-karolina-grabowska-4498136.jpg","datePublished":"2021-03-26T19:50:07+00:00","dateModified":"2021-03-26T19:50:12+00:00","description":"My solution for an Iron Scripter PowerShell scripting challenge to export and import a PowerShell working console session.","breadcrumb":{"@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8252\/answering-the-powershell-export-challenge\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jdhitsolutions.com\/blog\/powershell\/8252\/answering-the-powershell-export-challenge\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8252\/answering-the-powershell-export-challenge\/#primaryimage","url":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/03\/pexels-karolina-grabowska-4498136.jpg","contentUrl":"https:\/\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/03\/pexels-karolina-grabowska-4498136.jpg","width":160,"height":240},{"@type":"BreadcrumbList","@id":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8252\/answering-the-powershell-export-challenge\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"PowerShell","item":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},{"@type":"ListItem","position":2,"name":"Answering the PowerShell Export Challenge"}]},{"@type":"WebSite","@id":"https:\/\/jdhitsolutions.com\/blog\/#website","url":"https:\/\/jdhitsolutions.com\/blog\/","name":"The Lonely Administrator","description":"Practical Advice for the Automating IT Pro","publisher":{"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jdhitsolutions.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/jdhitsolutions.com\/blog\/#\/schema\/person\/d0258030b41f07fd745f4078bdf5b6c9","name":"Jeffery Hicks","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg","caption":"Jeffery Hicks"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/832ae5d438fdcfc1420d720cd1991307927de8a0b12f2342e81c30f773e21098?s=96&d=wavatar&r=pg"}}]}},"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":8107,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/8107\/scripting-challenge-meetup\/","url_meta":{"origin":8252,"position":0},"title":"Scripting Challenge Meetup","author":"Jeffery Hicks","date":"February 1, 2021","format":false,"excerpt":"As you probably know, I am the PowerShell problem master behind the challenges from the Iron Scripter site. Solving a PowerShell scripting challenge is a great way to test your skills and expand your knowledge. The final result is merely a means to an end. How you get there and\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2021\/02\/rubik.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":9018,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/9018\/an-iron-scripter-warm-up-solution\/","url_meta":{"origin":8252,"position":1},"title":"An Iron Scripter Warm-Up Solution","author":"Jeffery Hicks","date":"May 6, 2022","format":false,"excerpt":"We just wrapped up the 2022 edition of the PowerShell+DevOps Global Summit. It was terrific to be with passionate PowerShell professionals again. The culmination of the event is the Iron Scripter Challenge. You can learn more about this year's event and winner here. But there is more to the Iron\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":7680,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7680\/friday-fun-back-to-school-with-powershell\/","url_meta":{"origin":8252,"position":2},"title":"Friday Fun: Back to School with PowerShell","author":"Jeffery Hicks","date":"September 11, 2020","format":false,"excerpt":"For today's fun with PowerShell, I thought I'd share my solutions for a recent Iron Scripter challenge. If you aren't familiar with these challenges, and you should be, they are designed to test your PowerShell skills and hopefully help you learn something new. There are challenges for all skill levels\u2026","rel":"","context":"In &quot;Friday Fun&quot;","block_context":{"text":"Friday Fun","link":"https:\/\/jdhitsolutions.com\/blog\/category\/friday-fun\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/09\/get-cylindervolume-format.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/09\/get-cylindervolume-format.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/09\/get-cylindervolume-format.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/09\/get-cylindervolume-format.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":7489,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7489\/powershell-word-play\/","url_meta":{"origin":8252,"position":3},"title":"PowerShell Word Play","author":"Jeffery Hicks","date":"May 19, 2020","format":false,"excerpt":"A few weeks ago an Iron Scripter PowerShell challenge was issued that involved playing with words and characters. Remember, the Iron Scripter challenges aren't intended to create meaningful, production worthy code. They are designed to help you learn PowerShell fundamentals and scripting techniques. This particular challenge was aimed at beginner\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/doublechar.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/doublechar.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/doublechar.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/doublechar.png?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/doublechar.png?resize=1050%2C600&ssl=1 3x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/05\/doublechar.png?resize=1400%2C800&ssl=1 4x"},"classes":[]},{"id":7712,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7712\/answering-the-wsman-powershell-challenge\/","url_meta":{"origin":8252,"position":4},"title":"Answering the WSMan PowerShell Challenge","author":"Jeffery Hicks","date":"September 30, 2020","format":false,"excerpt":"Today, I thought I'd share my solution to a recent Iron Scripter challenge. I thought this was a fascinating task and one with a practical result.\u00a0 I'd encourage you to try your hand at the challenge and come back later to see how I tackled it. The challenge is not\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/09\/get-psremotesessionuser-4.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/09\/get-psremotesessionuser-4.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/09\/get-psremotesessionuser-4.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/09\/get-psremotesessionuser-4.png?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/09\/get-psremotesessionuser-4.png?resize=1050%2C600&ssl=1 3x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/09\/get-psremotesessionuser-4.png?resize=1400%2C800&ssl=1 4x"},"classes":[]},{"id":7559,"url":"https:\/\/jdhitsolutions.com\/blog\/powershell\/7559\/an-expanded-powershell-scripting-inventory-tool\/","url_meta":{"origin":8252,"position":5},"title":"An Expanded PowerShell Scripting Inventory Tool","author":"Jeffery Hicks","date":"June 19, 2020","format":false,"excerpt":"The other day I shared my code that I worked up to solve an Iron Scripter PowerShell challenge. One of the shortcomings was that I didn't address a challenge to include a property that would indicate what file was using a given command. I also felt I could do better\u2026","rel":"","context":"In &quot;PowerShell&quot;","block_context":{"text":"PowerShell","link":"https:\/\/jdhitsolutions.com\/blog\/category\/powershell\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/get-psscriptinventory.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/get-psscriptinventory.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/get-psscriptinventory.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/get-psscriptinventory.png?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/get-psscriptinventory.png?resize=1050%2C600&ssl=1 3x, https:\/\/i0.wp.com\/jdhitsolutions.com\/blog\/wp-content\/uploads\/2020\/06\/get-psscriptinventory.png?resize=1400%2C800&ssl=1 4x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/8252","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=8252"}],"version-history":[{"count":0,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/8252\/revisions"}],"wp:attachment":[{"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=8252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=8252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jdhitsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=8252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}