1

I tried to run a script from Bulk delete files from library using name of file

$webUrl = ''
$listName = ''
$query = '<Where>' +
    '<Contains>' +
    '<FieldRef Name="FileLeafRef"/><Value Type="Text">bad_file</Value>' +
    '</Contains>' +
    '</Where>'
$viewFields = '<FieldRef Name="FileRef"/><FieldRef Name="FileLeafRef"/>'
$delete = $false

Start-SPAssignment -Global

try {
    $web = Get-SPWeb $webUrl
    $list = $web.Lists[$listName]
    $spQuery = New-Object -TypeName Microsoft.SharePoint.SPQuery
    $spQuery.Query = $query
    $spQuery.ViewFields = $viewFields
    $spQuery.ViewFieldsOnly = $true
    $spQuery.ViewAttributes = 'Scope="Recursive"'
    $items = $list.GetItems($spQuery)
    $items | Select-Object ID, @{Name="FileName";Expression={$_["FileLeafRef"]}} |
        Export-Csv -Path ".\FilesToDelete.csv" -NoTypeInformation
    if ($delete) {
        $batch = '<?xml version="1.0" encoding="UTF-8"?>'
        $batch += '<ows:Batch>'
        foreach ($item in $items) {
            $batch += '<Method>'
            $batch += '<SetList Scope="Request">' + $list.ID + '</SetList>'
            $batch += '<SetVar Name="Cmd">Delete</SetVar>'
            $batch += '<SetVar Name="ID">' + $item.ID + '</SetVar>'
            $batch += '<SetVar Name="owsfileref">' + $item["FileRef"] + '</SetVar>'
            $batch += '</Method>'
        }
        $batch += '</ows:Batch>'
        $web.ProcessBatchData($batch)
    }
}
catch {
    Write-Error $_
}

Stop-SPAssignment -Global

And I get the following error:

C:\Users\admin\Documents\scripts\scripts.ps1 : The term 'Get-SPWeb' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the
path is correct and try again.

Is the function deprecated? If so, what replaces it?

3 Answers 3

1

The error usually comes if you directly run the command in the Windows PowerShell Console.

To resolve to execute the below command first:

Add-PSSnapin Microsoft.Sharepoint.Powershell

Ideally, we should run in SharePoint 2013 Management Shell rather than only in the PowerShell console. So Open SharePoint 2013 Management Shell in Administrator mode and then run the Cmdlet the error will not come.

If you are using the Windows PowerShell ISE to run, debug or test PowerShell cmdlets, then you can write the script like below:

Add-PSSnapin Microsoft.Sharepoint.Powershell
$web = Get-SPWeb “http://win-pfcp2dgt8di/sites/EnjoySharePoint/”

Reference URL :

https://www.enjoysharepoint.com/the-term-get-spweb-is-not-recognized/

Troubleshooting:

We may get the below error:

add-pssnapin : No snap-ins have been registered for Windows PowerShell version 5

Need to follow the below:

Please make sure that you run the PowerShell on a SharePoint Server machine.

The PowerShell script below can get a list of snap-ins that are registered on your computer:

get-pssnapin –registered

More information about Add-PSSnapin:

http://technet.microsoft.com/en-us/library/hh849705.aspx

For the details troubleshooting - please refer the below articles:

add-pssnapin : No snap-ins have been registered for Windows PowerShell version 5

Powershell Add-PSSnapin produces "No snap-ins have been registered for Windows Powershell version 4"

3
  • Add-PSSnapin : No snap-ins have been registered for Windows PowerShell version 5. At line:1 char:1 + Add-PSSnapin Microsoft.Sharepoint.Powershell + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (Microsoft.Sharepoint.Powershell:String) [Add-PSSnapin], PSArgumentExce ption + FullyQualifiedErrorId : AddPSSnapInRead,Microsoft.PowerShell.Commands.AddPSSnapinCommand
    – Sayaman
    Mar 6, 2020 at 13:32
  • Updated the answer - need to follow some troubleshooting for the above error
    – SP 2022
    Mar 6, 2020 at 13:54
  • Ah, do you absolutely run it on a powershell on a sharepoint server machine? How do you batch delete if you don't have a powershell running on a sharepoint server machine?
    – Sayaman
    Mar 6, 2020 at 14:00
3

You tag your thread as SharePoint online so I suppose you're using SharePoint online. Get-SPWeb is SharePoint module for on-premise only.

You could use PnP PowerShell or CSOM PowerShell instead.

1

Looks like you did not load the SharePoint module. Add this as the first line of your script.

Add-PSSnapin Microsoft.SharePoint.Powershell

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.