Adding Powershell to remove details from a link

Hi All,

I have a powershell query which extracts files of sharepoint and downloads them to a specified folder, for another system to combine with another file and emails or prints together.

However recently we upgraded our Sharepoint and now at the end of each URL there is often ?csf=xyz.

This recent addition is now causing the our scripts to not download the file.

I am not sure on how in my below code (this person has left the company) and im just trying to learn, but could not see anywhere on the correct method to do it.

There is 1 part of the script where data cleansing happens and would be good to know to do it.

$DownloadPathandName = $DownloadPath + $outputFile
$DownloadPathandName = $DownloadPathandName -replace “%20”," "
$DownloadPathandName = $DownloadPathandName -replace “t|n|`r”,""
$DownloadPathandName = $DownloadPathandName -replace “&”,"&"

Im just trying to get a strip everything after and include from “?csf=xxxxx”.

Hoping someone can help me out.

If you have a string where you want to remove ?csf=xxxx and everything after it, you can do the following:

$uri = 'https://sharepoint.domain.com/MyData?csf=xyz&SomethingElse'

$newuri = $uri -replace '\?csf=.*'

See Regex for an explanation of the regex match.

Another option is setting it as URI type to automatically parse the URI components.

[System.Uri]$uri = 'https://sharepoint.domain.com/MyData?csf=xyz&SomethingElse'

$uri.AbsoluteUri.Replace($uri.Query,'')

Output:

PS C:\Users\rasim> $uri


AbsolutePath   : /MyData
AbsoluteUri    : https://sharepoint.domain.com/MyData?csf=xyz&SomethingElse
LocalPath      : /MyData
Authority      : sharepoint.domain.com
HostNameType   : Dns
IsDefaultPort  : True
IsFile         : False
IsLoopback     : False
PathAndQuery   : /MyData?csf=xyz&SomethingElse
Segments       : {/, MyData}
IsUnc          : False
Host           : sharepoint.domain.com
Port           : 443
Query          : ?csf=xyz&SomethingElse
Fragment       : 
Scheme         : https
OriginalString : https://sharepoint.domain.com/MyData?csf=xyz&SomethingElse
DnsSafeHost    : sharepoint.domain.com
IdnHost        : sharepoint.domain.com
IsAbsoluteUri  : True
UserEscaped    : False
UserInfo       : 


PS C:\Users\rasim> $uri.AbsoluteUri.Replace($uri.Query,'')

https://sharepoint.domain.com/MyData

Thanking you both for the assistance.

 

This is now working correctly.

 

Cheers

Graeme