Skip to content

Instantly share code, notes, and snippets.

View ankitjoshi8518's full-sized avatar

Ankit Joshi ankitjoshi8518

  • Graphic International Centre
  • Abu Dhabi
View GitHub Profile
@ankitjoshi8518
ankitjoshi8518 / checkfilelockingandCloseit.ps1
Created November 9, 2021 06:17
Check if a file on windows file share is locked and forcefully close it
#sometimes if the file has been opened in Read+Write mode by excel, a temporary file will be created in the same directory starting with ~$filename.xlsx
#delete that temporary file which is in edit mode
Get-childitem -path C:\sharefolderpath
remove-item -path C:\sharefolderpath\~$File.xlsx
#check if the actual file is locked - This will also display the username and client IP along with fileID and sessionID
Get-SmbOpenFile | Where-Object -Property sharerelativepath -match "filename.XLSX" | fl
@ankitjoshi8518
ankitjoshi8518 / SearchADComputers.ps1
Created October 31, 2021 07:51
Get list of all computers (Windows Servers and Client OS) from AD
#Get all active computers as seeb by AD in last 50 days:
$Date = (Get-Date).AddDays(-49)
Get-ADComputer -Filter {LastLogonDate -gt $Date} | Select distinguishedName
#Get all computers from AD where OS version includes "server":
Get-ADComputer -Filter 'operatingsystem -like "*server*" -and enabled -eq "true"' `
-Properties Name,Operatingsystem,OperatingSystemVersion,IPv4Address |
Sort-Object -Property Operatingsystem |
@ankitjoshi8518
ankitjoshi8518 / disableIEENhancedSecurityonIE.ps1
Created October 10, 2021 12:22
Disable IE Enhanced Security
function Disable-IEESC {
$AdminKey = "HKLM:\SOFTWARE\Microsoft\Active Setup\Installed Components\{A509B1A7-37EF-4b3f-8CFC-4F3A74704073}"
$UserKey = "HKLM:\SOFTWARE\Microsoft\Active Setup\Installed Components\{A509B1A8-37EF-4b3f-8CFC-4F3A74704073}"
Set-ItemProperty -Path $AdminKey -Name "IsInstalled" -Value 0
Set-ItemProperty -Path $UserKey -Name "IsInstalled" -Value 0
Stop-Process -Name Explorer
Write-Host "IE Enhanced Security Configuration (ESC) has been disabled." -ForegroundColor Green
}
Disable-IEESC
@ankitjoshi8518
ankitjoshi8518 / getloggedonuser.ps1
Created August 5, 2021 12:16
Identify the user logged on a remote computer
Get-WmiObject -ComputerName DESKTOP1 -Class Win32_ComputerSystem | Select-Object UserName
@ankitjoshi8518
ankitjoshi8518 / UserOffboardingFromM365.ps1
Created July 31, 2021 11:38
Converting mailbox to shared, grant permissions and setup email forwarding
#Install-Module -Name ExchangeOnlineManagement
Connect-ExchangeOnline
$upn=Read-Host "Enter the email address the mailbox to be converted to shared mailbox"
$manager=Read-Host "Enter the email address of the MANAGER"
$access=Read-Host "Does manager need access to old emails? Y for yes and N for No"
Set-Mailbox -Identity $upn -Type Shared
if ($access -eq 'y')
@ankitjoshi8518
ankitjoshi8518 / CreateAzureCustomRole-ReadVMStatus.ps1
Created July 24, 2021 11:42
Create Azure Custom role using Powershell
#Run the below script to create new role "Virtual Machine Status" which allows the user to just view the Virtual Machine
#The most easiest way to get started is by checking the permissions to other role and modifying it as required
Get-AzureRmRoleDefinition -Name "Virtual Machine User Login"
(Get-AzureRmRoleDefinition -Name "Virtual Machine User Login").actions # list the actions that is possible with this role
$role= Get-AzureRmRoleDefinition -Name "Virtual Machine User Login" # copy the role to a variable
$role #shows the same output as line#3
#assign custom values to the new role that needs to be created