Posts (page 12)
- 7 min readTo run a PowerShell script as an admin, you can right-click on the Windows PowerShell icon and select "Run as administrator" to open an elevated PowerShell window. Then, navigate to the directory where your script is located using the "cd" command. Finally, enter the name of the script file followed by any required parameters to execute the script with administrative privileges.
- 5 min readTo call a method from a PowerShell script, you first need to create an object that contains the method you want to call. This can be done using the New-Object cmdlet or by creating a custom object. Once you have the object, you can use the dot notation to access and call the method. For example, if you have an object called $myObject and it has a method called MyMethod, you can call it like this: $myObject.MyMethod().
- 3 min readTo launch a 64-bit PowerShell session from a 32-bit cmd.exe session, you can follow these steps:Open a 32-bit cmd.exe session by typing "cmd" in the Windows search bar and hitting Enter. Navigate to the Sysnative directory by typing "cd C:\Windows\Sysnative" and hitting Enter. Once in the Sysnative directory, type "powershell.exe -Version 2.0" and hit Enter to launch a 64-bit PowerShell session.
- 4 min readTo catch exceptions in PowerShell, you can use the Try-Catch block. Surround the code that may throw an exception with the Try block, and catch the exception in the Catch block. This allows you to handle the exception gracefully and continue with the script execution without causing it to stop abruptly. You can also access information about the exception using the automatic variable $_ within the Catch block, allowing you to log or display relevant details about the error.
- 3 min readTo replace text inside braces using PowerShell, you can use the -replace operator along with regular expressions. You can define a pattern that matches text inside braces and then replace it with the desired text. Make sure to escape special characters in the regular expression pattern to ensure proper matching. Using this approach, you can easily replace text inside braces in a string using PowerShell.How to replace text inside braces based on user input in PowerShell.
- 4 min readTo get a custom assembly attribute with PowerShell, you can use the Get-ChildItem cmdlet to locate the assembly file, and then use the System.Reflection.Assembly class to load and inspect the attributes of the assembly.First, find the location of the assembly file by running Get-ChildItem -Path "C:\path\to\assembly".Next, load the assembly using [System.Reflection.Assembly]::LoadFrom("C:\path\to\assembly\assembly.dll").
- 5 min readTo check open ports using PowerShell, you can use the Test-NetConnection cmdlet in the PowerShell console. Simply open the PowerShell console and type Test-NetConnection -ComputerName <hostname or IP address> -Port <port number> to test for an open port on a specific machine. This command will return information about whether the port is open or closed.
- 4 min readTo add a child element for XML in PowerShell, you can first load the XML file using the [xml] type accelerator and then use the .AppendChild() method to add a new child element. You can also use the .CreateElement() method to create a new element and then append it to the parent element using the same .AppendChild() method. Finally, you can save the changes back to the XML file using the .Save() method.How to remove a child element from an XML document in PowerShell.
- 5 min readTo send date/time to a COM port via PowerShell, you first need to open and configure the COM port. You can use the .NET Framework's System.IO.Ports.SerialPort class to achieve this.Here is a basic example of how you can send the current date and time to a COM port using PowerShell: # Specify the COM port and baud rate $port = new-Object System.IO.Ports.SerialPort COM3,9600,N,None,8,1 # Open the COM port $port.
- 2 min readIn PowerShell, the equivalent of 'nohup' is using the 'Start-Process' cmdlet. This cmdlet allows you to start a process in the background and continue running even after the current session is closed. You can also use the '&' operator to run a command in the background. Additionally, you can use the 'Start-Job' cmdlet to run a scriptblock in the background as a job.
- 5 min readTo run a PowerShell script from a batch file, you can use the following command:powershell.exe -File "C:\path\to\your\script.ps1"Replace "C:\path\to\your\script.ps1" with the actual path to your PowerShell script.You can also run the script in a new PowerShell window by using the following command:powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "& 'C:\path\to\your\script.
- 5 min readTo get the NuGet output directory through PowerShell, you can use the following command: $projectPath = "path/to/your/project.csproj" $nugetOutputPath = (Get-Content $projectPath) -match "<OutputPath>(.*?)<\/OutputPath>" | Out-Null; $matches[1] This command retrieves the output path specified in the project file (typically a .csproj file) and stores it in the variable $nugetOutputPath. You can then use this variable to access the NuGet output directory.