Question: I have a script that will send items to the recycle bin (if selected) or delete items permanently. If the script is run locally, the recycle piece works properly.

However, if it's run from a different computer - in this case, my local machine runs the script against a shared folder on a server - the delete is **permanent**, and doesn't get sent to the recycle bin. The script (in a prior run) makes a decision about **WHAT** to delete by first setting the Archive bit to **TRUE** and then (after seeing how many backups it is to retain) un-setting the Archive bit for items to be deleted on the next execution of that same script. 

My thought was to alter the main script to mark the files for deletion, but only do the physical action of deleting the file(s) only when the script was being run locally, or to put the Recycle script (by itself) as a Task on the server that would delete & send the item to the Recycle Bin that would run at a set interval.

My questions-

1. In Powershell (using 2.0) how do you determine the source computer
vs the target computer? In this case, the script is being run from
MyPC, and it's target is Server1.
2. The script will run whether the target is a mapped drive (Drive Y:),
or if it's targeted by the servername (\Server1). How can you
distinguish the above question in both of these cases?

Answer: 
1. You can get the local computer name with $env:COMPUTERNAME. Use it to compare the value against the target server name.
2. For each file, you'd have to check first if the drive is a mapped drive, if it is, get the server name from the wmi instance and compare it to $env:COMPUTERNAME.

You can get a file's Drive qualifier with the `Split-Path` cmdlet:

```
PS> $drive = Split-Path Q:\test.txt -Qualifier
PS> $drive
Q:

```

And then get the server name with WMI:

```
PS> (gwmi win32_logicaldisk -filter "drivetype=4 and deviceid='$drive'").ProviderName.Split('\')[2]    
Server1

```
The OP wrote:

> 
> @Shay - Thanks for your help. I've learned a great deal from many posts by you on various Powershell sites.
> 
> 
> I was able to use almost everything you suggested, and only had to add an extra line of code to make it work. I checked the property `([System.Uri]$markedFile).IsUnc` to determine if the filename I've read is a UNC name.
> 
> 
> It returns False if the drive is mapped, and True if it is UNC. From that, I'm able to get the servername & make a comparison to the environment. Code follows.
> 
> 
> 
> ```
> $markedFile = "\\Server1\foldername1\Error.log"
> #$markedFile = "Y:\foldername1\Error.log"
> 
> $TargetComputer = $null
> $thisComputer = Get-Content env:computername
> if (Test-Path $markedFile) {  # if file exists
>     if (([System.Uri]$markedFile).IsUnc) { # if it's a UNC name & not a mapped drive name
>         $TargetComputer = ([System.Uri]$markedFile).Host
>         }
>     else {  #file is not a UNC name, it must be a mapped drive 
>         $drive = Split-Path $markedFile -Qualifier
>         $TargetComputer = (gwmi win32_logicaldisk -Filter "drivetype=4 and deviceid = '$drive'").Providername.split('\')[2]
>     }
> }
> 
> ```
> 
> The above code works either way. Thank you again for your help!
> 
> 
>