Showing posts with label Powershell. Show all posts
Showing posts with label Powershell. Show all posts

Wednesday, December 7, 2022

Delete Files and Subfolders from SharePoint Library using PowerShell

Make life easy with PowerShell, following script will help you to delete all files and subfolders in One Go.

Friday, January 28, 2022

Download SharePoint Online List items Attachments using PnP PowerShell

 Let’s download all attachments from a list. Following script creates a folder for every list object and downloads all attachments to the local disk.

Friday, March 23, 2018

Send Email with Powershell

Example1
# You must have Site Collection Access to use following process.

function SendEmail($requestor)
            {
                try
                {
                    $emailTo = $requestor
                    $emailSubject = "Test"
                    $emailBody = "Hi,This is a Test Email."

                    $emailUser = "yourEmail@myDomain.com"
                    $emailPwd = "pass@123"
                    
                    Send-PnPMail -To $emailTo -Subject $emailSubject -Body $emailBody -From $emailUser -Password $emailPwd
                    Write-output "Email sent: Successful!"
                }
                catch
                {
                    #known suppression
                    Write-output "Email sent: Failed! Reason:"$_.Exception.Message.ToString()
                }
            }
SendEmail("myEmail@myDomain.com")


Example 2
# Relay emailID example.

$smtp = new-object Net.Mail.SmtpClient("relay-mailbox.com") #Dns name or IP

$smtp.Send("fromEmail@myDomain.com", "toEmail@myDomain.com", "PowerShell Email", "This is a email from powershell")

Delete List Item with Excel Data Using Powershell


function DeleteListItem(){

    $inputfile = Get-FileName "c:\"
    if($inputfile -ne "")
    {
        $objExcel=New-Object -ComObject Excel.Application
        $objExcel.Visible=$false
        $WorkBook=$objExcel.Workbooks.Open($inputfile)
        $worksheet = $WorkBook.sheets.Item(1)
    
        ##########################
        $listName = "PnPList"

        $intRowMax = ($worksheet.UsedRange.Rows).count
        for($intRow = 2 ; $intRow -le $intRowMax ; $intRow++)
        {
            $ID = $worksheet.cells.item($intRow,1).value2
            Remove-PnPListItem -List $listName -Identity $ID -Force # -Force will delete without confirm
        }  
        $WorkBook.close()
        $objexcel.quit()

        ##########################
    }
}

Function Get-FileName($initialDirectory)
{
    [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
    
    $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
    $OpenFileDialog.initialDirectory = $initialDirectory
    $OpenFileDialog.filter = "xlsx (*.xlsx)| *.xlsx"
    $OpenFileDialog.ShowDialog() | Out-Null
    $OpenFileDialog.filename
}

DeleteListItem # Call Function

Update List Item with Excel Data Using Powershell



function UpdateListItem(){

    $inputfile = Get-FileName "c:\"
    if($inputfile -ne "")
    {
        $objExcel=New-Object -ComObject Excel.Application
        $objExcel.Visible=$false
        $WorkBook=$objExcel.Workbooks.Open($inputfile)
        $worksheet = $WorkBook.sheets.Item(1)
    
        ##########################
        $listName = "PnPList"

        $intRowMax = ($worksheet.UsedRange.Rows).count
        for($intRow = 2 ; $intRow -le $intRowMax ; $intRow++)
        {
            $ID = $worksheet.cells.item($intRow,1).value2
            $Title = $worksheet.cells.item($intRow,2).value2
            $Description = $worksheet.cells.item($intRow,3).value2

            $itemValue = @{"Title"=$Title;"Description"=$Description} #item value
            Set-PnPListItem -List $listName -Identity $ID -Values $itemValue            
        }  
        $WorkBook.close()
        $objexcel.quit()

        ##########################
    }
}

Function Get-FileName($initialDirectory)
{
    [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
    
    $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
    $OpenFileDialog.initialDirectory = $initialDirectory
    $OpenFileDialog.filter = "xlsx (*.xlsx)| *.xlsx"
    $OpenFileDialog.ShowDialog() | Out-Null
    $OpenFileDialog.filename
}

UpdateListItem # Call Function

Create List Item with Excel Data Using Powershell


function AddListItem(){

    $inputfile = Get-FileName "c:\"
    if($inputfile -ne "")
    {
        $objExcel=New-Object -ComObject Excel.Application
        $objExcel.Visible=$false
        $WorkBook=$objExcel.Workbooks.Open($inputfile)
        $worksheet = $WorkBook.sheets.Item(1)
    
        ##########################
        $listName = "PnPList"

        $intRowMax = ($worksheet.UsedRange.Rows).count
        for($intRow = 2 ; $intRow -le $intRowMax ; $intRow++)
        {
            $Title = $worksheet.cells.item($intRow,1).value2
            $Description = $worksheet.cells.item($intRow,2).value2

            $itemValue = @{"Title"=$Title;"Description"=$Description} #item value
            Add-PnPListItem -List $listName -Values $itemValue
        }  
        $WorkBook.close()
        $objexcel.quit()

        ##########################
    }
}
# Below function for Dialog Box to select file.
Function Get-FileName($initialDirectory)
{
    [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
    
    $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
    $OpenFileDialog.initialDirectory = $initialDirectory
    $OpenFileDialog.filter = "xlsx (*.xlsx)| *.xlsx"
    $OpenFileDialog.ShowDialog() | Out-Null
    $OpenFileDialog.filename
}

AddListItem # Call Function