Found 461 Questions for PowerShell

How to get the Shared folder permissions with PowerShell?

Chirag Nagrekar
Updated on 01-Mar-2021 10:47:51
To get the shared folder permissions using PowerShell, we can use the Get-SmbShare cmdlet.For example, we have a shared folder name DSC and we need to retrieve its permissions, we can use the below command.CommandGet-SmbShare -Name DSCOutputName ScopeName Path Description ---- --------- ---- ----------- DSC       *   E:\DSCIt doesn’t show the permission by default and we can retrieve the full list using Fl *. For example, Get-SmbShare -Name DSC | fl *And you can see the PresentPathACL property there. This property is used to retrieve the permissions on the shared folder. So we can directly use the command, Command(Get-SmbShare -Name ... Read More

How to change SMB shared folder access permission using PowerShell?

Chirag Nagrekar
Updated on 01-Mar-2021 10:46:23
We can use the command Grant-SmbShareAccess to change the shared folder access permission or to assign the new user to the shared folder with permission.In this example, we have already shared a folder name called “Shared Folder” and everyone's user permission is assigned with the read access and we will change it to the Full access permission.PS C:\Temp> Get-SmbShareAccess -Name "Shared folder" Name          ScopeName AccountName AccessControlType AccessRight ----          --------- ----------- ----------------- ----------- Shared folder    *       Everyone          Allow         ReadExampleTo change the permission ... Read More

How to share a windows folder using PowerShell?

Chirag Nagrekar
Updated on 01-Mar-2021 10:45:13
To share a windows folder using PowerShell, we can use the New-SmbShare command. This command is a part of the module SmbShare.In this example, we have a folder called “DSC” and we want to share. The below command will simply share folderNew-SmbShare -Path E:\DSC\ -Name "Shared Folder"OutputName          ScopeName Path Description ----          --------- ---- ----------- Shared Folder    *      E:\DSCDSC folder will be shared with a “Shared Folder” name with everyone’s Read Permission by default because we haven’t specified the scope yet.To assign the Full Access permission to the specific user, ... Read More

How to update the specific node of the XML file using PowerShell?

Chirag Nagrekar
Updated on 01-Mar-2021 12:09:27
To update the specific XML node using PowerShell, we first need to select that node with the attribute with SelectSingleNode() method.We have below the XML file from the link stored in SampleXml.XML on C:\Temp location.https://docs.microsoft.com/en-us/previous-versions/windows/desktop/ms762271(v=vs.85)In this example, we are going to update Autor and Genre properties of the Book having attribute Id = ‘bk102’$xml=[xml](Get-Content C:\Temp\SampleXML.xml)$node=$xml.SelectSingleNode("//book[@id='bk102']")The above commands will load the XML file and select node with attribute value ‘bk102’.$node.genre='Non-Fiction' $node.author='Dell James' $xml.Save("C:\Temp\SampleXML.xml")The above commands will update the genre and author property.Read More

How to delete the specific node from the XML file using PowerShell?

Chirag Nagrekar
Updated on 01-Mar-2021 09:59:35
To delete the specific XML node from the PowerShell, we can use the RemoveChild() method of the XML.For example, We have a sample XML file from Microsoft.https://docs.microsoft.com/en-us/previous-versions/windows/desktop/ms762271(v=vs.85)We have saved the above file into C:\Temp\SampleXml.XML and we need to delete the book node with attribute ‘bk102’ and for that, we will use the XPath method of the XML.Below commands will first search the XML book node with the book attribute ‘bk102’ and then we will delete it.$xml = [xml](Get-Content C:\Temp\SampleXML.xml) $node = $xml.SelectSingleNode("//book[@id='bk102']") $node.ParentNode.RemoveChild($node) | Out-Null $xml.Save('C:\Temp\SampleXML.xml')If you want to delete all the nodes which have the name “Book”, we can ... Read More

How to add an attribute to the XML file using Powershell?

Chirag Nagrekar
Updated on 01-Mar-2021 09:56:31
To add the attribute to the XML, we first need to add the Element and then we will add an attribute for it.Below is a sample of the XML file.Example           Gambardella, Matthew       XML Developer's Guide       Computer       44.95       2000-10-01       An in-depth look at creating applications with XML.     The below command will add the new element but the file won’t be saved. We will save the file after the attribute is added.$file = "C:\Temp\SampleXML.xml" $xmlfile = [xml](Get-Content $file) $newbookelement ... Read More

How to add a new element in the XML using PowerShell?

Chirag Nagrekar
Updated on 19-Feb-2021 13:18:54
Suppose we have a XML file as shown below.           Gambardella, Matthew       XML Developer's Guide       Computer       44.95       2000-10-01       An in-depth look at creating applications with XML.     We need to add a new node. So we will first load the XML file and then operate on it as shown below.The below command will save the XML file to the variable.$xmlfile = [XML](Get-Content C:\Temp\SampleXML.xml)The below command will create a new XML element$newelement = $xmlfile.CreateElement("book")Once the element is created we need ... Read More

How to remove Windows service with PowerShell?

Chirag Nagrekar
Updated on 19-Feb-2021 13:13:54
We need to remove the window service named TestService using PowerShell. If you are using PowerShell 6.0 or above version, you can directly use a cmdlet Remove-Service command as shown below.In this example, we have a service name called TestService.Remove-Service Testservice -Confirm:$false -VerboseIf you are using the PowerShell framework version (5.1 or below), you need to use the registry. Services are stored in the registry at the below location.HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\To delete the service, we need to remove that service key with the command as shown below.Get-Item HKLM:\SYSTEM\CurrentControlSet\Services\TestService | Remove-Item -Force -Verbose Here we are using the Service name TestService and you need to reboot the server ... Read More

How to run Invoke-Command in PowerShell Workflow?

Chirag Nagrekar
Updated on 19-Feb-2021 13:15:55
To run Invoke-Command in PowerShell Workflow we need to use the InlineScript block because Invoke-Command is not supported directly in the workflow. The below example is without using the InlineScript block we get an error.ExampleWorkflow TestInvokeCommand{    Invoke-Command -ComputerName LabMachine2k16 -ScriptBlock{       Get-Service WINRM    } }Output −At line:2 char:5 +    Invoke-Command -ComputerName LabMachine2k16 -ScriptBlock{ +    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Cannot call the 'Invoke-Command' command. Other commands from this module have been packaged as workflow activities, but this command was specifically excluded. This is likely because the command requires an interactive Windows PowerShell session, or has behavior not suited ... Read More

How Parallel and Sequence execution works in PowerShell Workflow?

Chirag Nagrekar
Updated on 19-Feb-2021 13:08:19
PowerShell workflows are the best way to design the script to execute on more than one node parallel which saves extensive time for the output to produce but we always don’t want to run all the commands parallel but also need some of them to run sequentially and we can design both Parallel and Sequence commands using PowerShell Workflow.Workflow TestWorkflow{    parallel{       Command1       Command2    }    Sequence{       Command3       Command4    } } TestWorkflowIn the above code, Command1, Command2 will be executed parallelly in any order while command3 ... Read More
Advertisements