# Powershell Session File Transfer

### ⚡ PowerShell Remoting File Transfers – When HTTP/SMB Aren’t Available

Need to move files but **HTTP, HTTPS, or SMB are blocked**? 😱\
No worries — **PowerShell Remoting (WinRM)** has your back! 🛡️

With **PowerShell sessions**, you can **send and receive files** from remote systems using built-in cmdlets like `Copy-Item`. 🧳📁

***

#### 🧠 What is PowerShell Remoting?

PowerShell Remoting allows you to:

* Run commands/scripts **on remote systems**
* Transfer files between **your machine and the remote**
* Use **sessions** for persistent connections

It works over:

* 📡 HTTP (port 5985)
* 🔐 HTTPS (port 5986)

By default, enabling PowerShell Remoting creates listeners on both!

***

### Step 1: Confirm Who You Are & Where You Are

```powershell
PS C:\htb> whoami
htb\administrator

PS C:\htb> hostname
DC01
```

You're logged in as `Administrator` on **DC01** ✅

***

### Step 2: Confirm Remote Host is Reachable (WinRM Port 5985)

```powershell
Test-NetConnection -ComputerName DATABASE01 -Port 5985
```

📌 Output:

```
ComputerName     : DATABASE01
RemoteAddress    : 192.168.1.101
RemotePort       : 5985
TcpTestSucceeded : True
```

✅ We can connect — WinRM is open and listening!

***

### Step 3: Create a Remote PowerShell Session

```powershell
$Session = New-PSSession -ComputerName DATABASE01
```

📌 If you're already `Administrator` on the remote machine, no credentials needed!

***

### Step 4: Transfer File FROM Local ➡️ Remote

{% code overflow="wrap" %}

```powershell
Copy-Item -Path C:\samplefile.txt -ToSession $Session -Destination C:\Users\Administrator\Desktop\
```

{% endcode %}

📁 This copies `samplefile.txt` from **DC01** to **DATABASE01**.

***

### Step 5: Transfer File FROM Remote ➡️ Local

{% code overflow="wrap" %}

```powershell
Copy-Item -Path "C:\Users\Administrator\Desktop\DATABASE.txt" -Destination C:\ -FromSession $Session
```

{% endcode %}

📁 This pulls `DATABASE.txt` from **DATABASE01** and saves it to `C:\` on **DC01**.

***

### Optional Cleanup: Close the Session

```powershell
Remove-PSSession $Session
```

💡 Always good practice to close what you open. 🔐

***
